LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-11-2007, 07:26 PM   #1
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
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).

Last edited by Robhogg; 02-11-2007 at 07:31 PM.
 
Old 02-11-2007, 09:34 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

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

Last edited by graemef; 02-11-2007 at 09:38 PM.
 
Old 02-11-2007, 09:37 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
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])
     . 
"\");";
?>
 
Old 02-12-2007, 07:52 AM   #4
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 85
Thanks muchly - trim (or rather ltrim) was what I needed.

Yours,
Rob
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript array to PHP using cookies problem climbingmerlin Programming 10 03-09-2006 10:06 AM
Line breaks aren't interpreted when defining a variable spiffytech Linux - Software 4 12-27-2005 03:08 PM
Need REG EXP Help to remove line breaks webshark Programming 18 07-08-2005 04:31 PM
HTML frames javascript php problem rblampain Programming 4 04-08-2005 02:51 AM
MDK adds unwanted line to fstab kmasaryk Mandriva 4 11-17-2004 09:14 PM


All times are GMT -5. The time now is 12:58 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration