简体   繁体   English

php搞砸了我的JavaScript,按钮无法打开

[英]Php is messing up my javascript, button won't open

I have a javascript button on my site that is supposed to popup a URL at random from an external txt file of urls (one per line) but the php keeps messing up the javascript by inserting an extra break. 我的网站上有一个javascript按钮,应该从网址的外部txt文件中随机弹出一个URL(每行一个),但是php会通过插入额外的中断来弄乱javascript。

 <a href="#" onClick="javascript:window.open('http://something.com/a.php?u=<?php echo $url ?>', 'yea', 'height=520, width=400, location=no, menubar=no, resizable=no, scrollbars=no, status=no, titlebar = yes, toolbar=no');"><img src="clic.jpg" border="0"></a>

<?php 

$urls = @file('urls.txt');
$num = count($urls)-1;
$url = $urls[rand(0,$num)];

?> 

像这样使用file()
@file( 'urls.txt', FILE_IGNORE_NEW_LINES );

file() reads the entire file in to an array WITH the newlines still attached. file()将整个文件读入一个仍附加换行符的数组。

Try this: 尝试这个:

$url = trim($urls[rand(0,$num)]);

Try this: 尝试这个:

$url = trim($urls[rand(0,$num)]);

Each line has the CR (and LF) characters that mark a new line at its end. 每行都有CR(和LF)字符,在其末尾标记新行。 The trim function will remove such extra whitespace characters from the string. trim功能将从字符串中删除此类多余的空格字符。

<?php 

$urls = @file('urls.txt');
$num = count($urls)-1;
$url = $urls[rand(0,$num)];
$url = trim($url);
?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM