0

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.

 <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)];

?> 
5
  • You are missing a ; on your first <?php echo ... ?> Commented Jan 11, 2012 at 18:37
  • if that echo is the only statement inside the php code block the ; can be omitted Commented Jan 11, 2012 at 18:40
  • I doubt it since one's server-side and one's client-side. Commented Jan 11, 2012 at 18:42
  • This code is exactly as you posted? because you are echoing $url before you assign a value to it. Commented Jan 11, 2012 at 18:45
  • Also if you pass the url as a GET parameter remember to encode it properly. Commented Jan 11, 2012 at 18:47

4 Answers 4

1

Use file() like this:
@file( 'urls.txt', FILE_IGNORE_NEW_LINES );

Sign up to request clarification or add additional context in comments.

Comments

0

file() reads the entire file in to an array WITH the newlines still attached.

Try this:

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

Comments

0

Try this:

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

Each line has the CR (and LF) characters that mark a new line at its end. The trim function will remove such extra whitespace characters from the string.

Comments

0
<?php 

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.