Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

printing Javscript safe text by Removing line breaks

Posted on 2006-04-12
9
Medium Priority
?
277 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:jbclelland
  • 5
  • 4
9 Comments
 
LVL 3

Expert Comment

by:dancablam
ID: 16436447
Have you tried str_replace with "\r\n" to remove Window's carriage returns?
0
 

Author Comment

by:jbclelland
ID: 16437271
Yes i have tried this but with no success.
0
 
LVL 3

Expert Comment

by:dancablam
ID: 16437373
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jbclelland
ID: 16437459
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
0
 
LVL 3

Accepted Solution

by:
dancablam earned 2000 total points
ID: 16437567
Glad to hear it! The preg_replace uses a Regular Expression engine like that used by Perl (thus the p in 'preg'). str_replace really only replaces one thing at a time (unless you give it an array), and is kind of slow. Whereas the regex engine can be used to find/replace very complex patterns. I use it epsecially when parsing code that needs to be manipulated carefully. There is a LOT to regexes, but if you want to learn more about them, and create them quickly, I suggest downloading RegExBuddy (www.regexbuddy.com), which is a great tool for starters to make their own expressions.

Hope this helps!
Dan
0
 

Author Comment

by:jbclelland
ID: 16437630
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
0
 
LVL 3

Expert Comment

by:dancablam
ID: 16437755
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
0
 

Author Comment

by:jbclelland
ID: 16437816
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?
0
 
LVL 3

Expert Comment

by:dancablam
ID: 16438166
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++;
   }
}

?>
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question