Jump to content

[SOLVED] String manipulation problem


hasanatkazmi

Recommended Posts

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; ?>';

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 !!!!!!

Link to comment
Share on other sites

$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???

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

 

;D 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 :D

 

calm down ;D

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.