hasanatkazmi Posted January 30, 2008 Share Posted January 30, 2008 I have a file which basically outputs some javascript, the problem is that when the $html variable contains some thing with enter / newline, the code fails giving no error e.g if $html is like $html='<p><b>this works</b></p>'; but this fails $html='<p> <b> this works </b> </p>'; <?php the actual code is this : (which fails ) $html = '<p>this page talks about services provided by pixeltouch</p> <p> </p> <p> </p> <p>lolz</p> <p> </p> <p> </p> <p> </p> <p>we make graphics</p>'; ?> div = document.getElementById('nameofdiv'); div.innerHTML = '<?php echo $html; ?>'; Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/ Share on other sites More sharing options...
resago Posted January 30, 2008 Share Posted January 30, 2008 javascript is line sensitive. if you want javascript to output linefeeds, do \n\r or \n in php, to pass to javascript, you can do literal '\n\r' or '\n', but not "\n\r" or "\n" Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453944 Share on other sites More sharing options...
hasanatkazmi Posted January 30, 2008 Author Share Posted January 30, 2008 javascript is line sensitive. if you want javascript to output linefeeds, do \n\r or \n in php, to pass to javascript, you can do literal '\n\r' or '\n', but not "\n\r" or "\n" Perhaps you didnt get what i want to say, i dont want newline, i want to echo $html as innerHTML . the problem is that when $html contains a newline created by enter tab not by <br> or \n , it fails to work Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453946 Share on other sites More sharing options...
resago Posted January 30, 2008 Share Posted January 30, 2008 read my post again. javascript is line sensitive. if you echo from php, javascript considers the statement terminated at the first "enter" Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453949 Share on other sites More sharing options...
hasanatkazmi Posted January 30, 2008 Author Share Posted January 30, 2008 read my post again. javascript is line sensitive. if you echo from php, javascript considers the statement terminated at the first "enter" so how to solve it, how to convert newlines into noting, or simply doing what "wrap text" in note pad does? Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453952 Share on other sites More sharing options...
resago Posted January 30, 2008 Share Posted January 30, 2008 does it matter? Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453953 Share on other sites More sharing options...
hasanatkazmi Posted January 30, 2008 Author Share Posted January 30, 2008 does it matter? well, it does see, if (!isset($_GET['id'])) { die(); } else { $id = $_GET['id']; } switch ($id){ case "contentdiv1": $html = 'This should idealy work but isnt working'; break; case "contentdiv2": $html = '<b>This content came from our Ajax Engine and came for tag 2 </b>'; break; case "contentdiv3": $html = '<b>This content came from our Ajax Engine and came for tag 3 </b>'; break; } in the case of contentdiv1 , it gives no result (and no js error) , i dont know why !!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453954 Share on other sites More sharing options...
resago Posted January 30, 2008 Share Posted January 30, 2008 if this is coming from ajax, you need to change any "enters" IE \n\r with literals '\n\r' when js executes, it will convert them back to "enters" Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453957 Share on other sites More sharing options...
resago Posted January 30, 2008 Share Posted January 30, 2008 $html = 'This should idealy work but isnt working'; should be $html = 'This should idealy work \n\r but isnt working'; Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453960 Share on other sites More sharing options...
Barand Posted January 30, 2008 Share Posted January 30, 2008 If you want line breaks in a browser page you need '< br/> and not \n $html='<p> <br/> this works <br/> </p>'; Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453963 Share on other sites More sharing options...
resago Posted January 30, 2008 Share Posted January 30, 2008 you can't output that in javascript though, as in his innerhtml= statement. <script> whatever.innerHTML='<p> <br/> this works <br/> </p>'; will fail. Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453966 Share on other sites More sharing options...
hasanatkazmi Posted January 30, 2008 Author Share Posted January 30, 2008 $html = 'This should idealy work but isnt working'; should be $html = 'This should idealy work \n\r but isnt working'; yes this is exactly i should do, i have a number of long files, i cant change all 'enters' to '\\n\\r , how can i change all 'enters' by using some function, if i use str_replace than how can i tell the function what 'enter' is??? Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453970 Share on other sites More sharing options...
resago Posted January 30, 2008 Share Posted January 30, 2008 either "/n/r" or "/r" or "/n" or chr() Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453973 Share on other sites More sharing options...
Barand Posted January 30, 2008 Share Posted January 30, 2008 you can't output that in javascript though, as in his innerhtml= statement. <script> whatever.innerHTML='<p> <br/> this works <br/> </p>'; will fail. try <html> <head> <meta name="generator" content="PhpED Version 4.5 (Build 4513)"> <title>sample</title> <meta name="author" content="Barand"> <script> function showText() { var obj = document.getElementById("linebreaks"); obj.innerHTML = "Oh<br/>yes<br/>you<br/>can"; } </script> </head> <body onload="showText()"> <div id="linebreaks"></div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453976 Share on other sites More sharing options...
resago Posted January 30, 2008 Share Posted January 30, 2008 here we go: str_replace("\n",'\n',$data); str_replace("\r",'\r',$data); str_replace("\t",'\t',$data); this SHOULD convert newline;carriage return; and tab. Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453978 Share on other sites More sharing options...
resago Posted January 31, 2008 Share Posted January 31, 2008 you can't output that in javascript though, as in his innerhtml= statement. <script> whatever.innerHTML='<p> <br/> this works <br/> </p>'; will fail. try <html> <head> <meta name="generator" content="PhpED Version 4.5 (Build 4513)"> <title>sample</title> <meta name="author" content="Barand"> <script> function showText() { var obj = document.getElementById("linebreaks"); obj.innerHTML = "Oh<br/>yes<br/>you<br/>can"; } </script> </head> <body onload="showText()"> <div id="linebreaks"></div> </body> </html> yes, I know, br isn't the problem, its the linebreaks in $data that is throwing off his javascript because javascript terminates on line breaks like VB calm down Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453980 Share on other sites More sharing options...
hasanatkazmi Posted January 31, 2008 Author Share Posted January 31, 2008 great! it worked, u solved the biggest problem i faced in months Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453986 Share on other sites More sharing options...
resago Posted January 31, 2008 Share Posted January 31, 2008 Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453988 Share on other sites More sharing options...
Barand Posted January 31, 2008 Share Posted January 31, 2008 For the benefit of others, what was your final solution? Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-453995 Share on other sites More sharing options...
resago Posted January 31, 2008 Share Posted January 31, 2008 I'll summarize. poster has getting raw html into a var in php, lets call it $html. it contained newlines "/n", carriage returns "/r" and tabs "/t" which is all fine and dandy. but then they were using it in a JS script by using echo "whatver.innerHTML='$data';"; this presents a problem because the newline characters in $data are interpreted javascript as if you tried to do a multi-line statement. JS terminates on a ;newline combo (wierd). so the results were not as expected. to solve, we turn the special characters into literals that JS can handle and interpret. so in php, $html=contents of some file or url str_replace("\n",'\n',$html); str_replace("\r",'\r',$html); str_replace("\t",'\t',$html); echo "<script>"; echo "whatver.innerHTML='$html';"; echo '</script>'; this now works because the newlines are the literal '/n' etc... JS converts the literal back into newline for display. we could have just removed all newlines, etc.. but we want to preserve data integrity. Quote Link to comment https://forums.phpfreaks.com/topic/88647-solved-string-manipulation-problem/#findComment-454094 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.