LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP and JavaScript: problem with echo()-ing unwanted line breaks (https://www.linuxquestions.org/questions/programming-9/php-and-javascript-problem-with-echo-ing-unwanted-line-breaks-527660/)

Robhogg 02-11-2007 07:26 PM

PHP and JavaScript: problem with echo()-ing unwanted line breaks
 
Hi, All.

I'm trying to use PHP to read data from file, and then write it into a JavaScript array (which will be used to create a "news-ticker" bar on the page). The problem is that the PHP echo() function is inserting unwanted line breaks after the quote-mark which starts a string, leading to an "unterminated string literal" error. Example:
Code:

var storyLinks = new Array( "<?php global $lastThree; echo($lastThree[1][1]); ?>",
"<?php global $lastThree; echo($lastThree[2][1]); ?>",
"<?php global $lastThree; echo($lastThree[0][1]); ?>");

Produces the following:
Code:

var storyLinks = new Array( "
http://news.independent.co.uk/world/fisk/article2251354.ece",
"
http://news.bbc.co.uk/1/hi/world/americas/6348049.stm",
"
http://comment.independent.co.uk/commentators/article2258798.ece");

I also get a very similar result if I use a single PHP echo() statement, echo()-ing the quotes and commas as well.

Does anyone know a solution to this, please?

Thanks,
Rob

PS - phpinfo() tells me that I'm using PHP 4.4.4 on Apache 1.3.33 (if that makes a difference).

graemef 02-11-2007 09:34 PM

PHP Code:

<?php
global $lastThree;
echo 
"var storyLinks = new Array( \"{$lastThree[1][1]}\", \"{$lastThree[2][1]}\", \"{$lastThree[0][1]}\");";
?>


graemef 02-11-2007 09:37 PM

and if that doesn't work:
PHP Code:

<?php
global $lastThree;
echo 
"var storyLinks = new Array( \"" 
     
trim($lastThree[1][1]) 
     . 
"\", \""
     
trim($lastThree[2][1])
     . 
"\", \""
     
trim($lastThree[0][1])
     . 
"\");";
?>


Robhogg 02-12-2007 07:52 AM

Thanks muchly - trim (or rather ltrim) was what I needed.

Yours,
Rob


All times are GMT -5. The time now is 06:36 AM.