Link to home
Start Free TrialLog in
Avatar of jbclelland
jbclelland

asked on

printing Javscript safe text by Removing line breaks

Hi, I have some text loaded from a text field from my database.  I am printing it into a javascript array, which has worked in the past (when the text field was a varchar field) but now it fails. The problem is occuring when I print line breaks, as you can see from the javascript error below:

Error: unterminated string literal
Source File: http://manager.dev/manager/xref/xref_controls_edit.php?type=process&id=12&qid=19
Line: 118, Column: 8
Source Code:
0267] = '1.2. Choose a sample of (insert sample size) firewalls/routers,

The PHP that I am using to print my javsacript array is as follows:

for ($x=0; $x<count($data); $x++) {
      if ($data[$x]['element_type'] == 'CTL') {
            $cid = $data[$x]['question_id'];
            $js_controls .= "arrCTLs[$cid] = '{$data[$x]['tag']} ".trim($data[$x]['title'])."...';";  /******THIS LINE FAILS******/
            $js_controls .= "arrCTLpos[$cid] = {$data[$x]['full_order']};";
      }
}

The line marked above produces bad javascript, because the $data[$x]['title'] variable contains line breaks which makes the javascript fall over.  I have tried all the usual tricks, such as using nl2br and str_replace to remove line breaks (\n), but the javascript source still contains line breaks.  I cant get rid of the line breaks, and hence cant get my javascript to work properly.

The $data[$x]['title'] variable makes its way onto the screen in a multiple row list box, if the user ticks the appropriate tick box to put it there.  Does anyone have any ideas on how to get rid of my line breaks so that the javascript doesnt fall over?

Here is a snippet of the generated source code as well, so that you can see where the error is occuring:

/********SOURCE*******/
arrSelected[2]=10257;                  
                  lookup[10256] = Array('10257','10267','10271','10282','10285'); lookup[10257] = false; lookup[10267] = false; lookup[10271] = false; lookup[10282] = false; lookup[10285] = false; lookup[10287] = Array('10288','10290','10300'); lookup[10288] = false; lookup[10290] = false; lookup[10300] = false; lookup[10304] = Array('10305','10310','10314','10316','10322','10325'); lookup[10305] = false; lookup[10310] = false; lookup[10314] = false; lookup[10316] = false; lookup[10322] = false; lookup[10325] = false; lookup[10336] = Array('10337','10342'); lookup[10337] = false; lookup[10342] = false; lookup[10346] = Array('10347','10349'); lookup[10347] = false; lookup[10349] = false; lookup[10353] = Array('10354','10356','10358','10367','10372'); lookup[10354] = false; lookup[10356] = false; lookup[10358] = false; lookup[10367] = false; lookup[10372] = false; lookup[10383] = Array('10384','10389'); lookup[10384] = false; lookup[10389] = false; lookup[10394] = Array('10395','10397','10400','10402','10405'); lookup[10395] = false; lookup[10397] = false; lookup[10400] = false; lookup[10402] = false; lookup[10405] = false; lookup[10433] = Array('10434','10440','10443','10447','10450','10453','10455','10458','10460','10464'); lookup[10434] = false; lookup[10440] = false; lookup[10443] = false; lookup[10447] = false; lookup[10450] = false; lookup[10453] = false; lookup[10455] = false; lookup[10458] = false; lookup[10460] = false; lookup[10464] = false; lookup[10468] = Array('10469','10471','10479','10486','10488','10494','10497'); lookup[10469] = false; lookup[10471] = false; lookup[10479] = false; lookup[10486] = false; lookup[10488] = false; lookup[10494] = false; lookup[10497] = false; lookup[10500] = Array('10501','10505','10509','10512','10515'); lookup[10501] = false; lookup[10505] = false; lookup[10509] = false; lookup[10512] = false; lookup[10515] = false; lookup[10517] = Array('10518','10523','10525','10536','10538','10545','10548','10551','10557'); lookup[10518] = false; lookup[10523] = false; lookup[10525] = false; lookup[10536] = false; lookup[10538] = false; lookup[10545] = false; lookup[10548] = false; lookup[10551] = false; lookup[10557] = false;                  
                  arrCTLs[10257] = '1.1. Obtain and inspect the firewall configuration standards and other documentation specified below to obtain evidence the standards are complete.  Also obtain a copy of the following documentation:...';arrCTLpos[10257] = 2;arrCTLs[10267] = '1.2. Choose a sample of (insert sample size) firewalls/routers,
1.      between the Internet and the DMZ, and

/********END SOURCE*******/

Lots of points due to tight time scale.

Cheers

Ashley
Avatar of dancablam
dancablam

Have you tried str_replace with "\r\n" to remove Window's carriage returns?
Avatar of jbclelland

ASKER

Yes i have tried this but with no success.
Hi Ashley,
Let's try a regex to see if that will help.

Try:
preg_replace('/[\\r\\n]/', '', $data[$x]['title'])

If there's no need for any other whitespace chars including spaces, tabs, etc., then try:
preg_replace('/[\\s]/', '', $data[$x]['title'])

If that still doesn't work - could you give me an example of some of the text from the DB that's being pulled?

Thanks!
Dan
That works brilliantly, but could you explain exactly how?! ( I added in to remove \\t as well, and it works fine)

I dont understand the preg_replace function and its expressions!

Ashley
ASKER CERTIFIED SOLUTION
Avatar of dancablam
dancablam

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok ill give you the point just for that!

However, can you tell me if I ask another question if you could answer it, then ill set up another question?

We are in the process of taking our software product written in php and removing all the English text and replacing it with defines, so that we can take the definitions file and make it multi-lingual.  

Is there a regular expression that you could create, and some code to go with it, that will scan our source code tree and try and find strings of english text that we may have missed when we manually went through the product scripts ourselves?

Ashley
Thanks!

I'm afraid that might be a little outside Regex's scope. PHP could be used to detect char sets and mime types, so you could distinguish between english chars and say, Chinese symbols. But other languages that use english chars, in the standard ASCII range wouldn't really be detectable (french, italian, spanish, etc). You could traverse through all the words and compare them against a database of all english words. Just download a text file with all english words and put them into a SQL DB, explode each word out and see if the word exists. If it does, you could look to see if you've missed something.

Best regards,
Dan
What I really meant was to search for strings of text that dont contain any php type symbols such as the $, { and } signs etc.  I.e strings of text with three or more words without a $ sign are more likely to be english sentences.  Also, if they are on the same line, and dont contain php common function keywords such as 'for' etc.

Regex must be capable of this?
Ok, I've written you a little script that may be what you need. While it can't tell english from any other language, it does list line by line all strings inside of the script. Lemme know if that helps.

<?

//set filename to script you wish to parse
$code = file_get_contents('filename.php');

getTokens($code);

function getTokens($code) {
   $tokens = token_get_all($code);

   //list of tokens to include. Complete list can be found at: http://us3.php.net/manual/en/tokens.php
   $lookfor = array(T_CONSTANT_ENCAPSED_STRING,
                    T_STRING,
                    T_INLINE_HTML);

   //go through each line and echo any occurance of the given tokens
   $x = 1;
   foreach($tokens AS $token) {
      if(in_array($token[0], $lookfor)) {
         echo "<b>Line ".$x.":</b> ".htmlentities($token[1])."<br>\n";
      }
      $x++;
   }
}

?>