Page 1 of 1

line break stripping in php for javascript

Posted: Wed Jul 23, 2003 8:52 pm
by oculasm
hello.. Im having a bit of an issue stripping what I believe are line breaks..from my php array elements..Ive had a shot at rtrim and ltrim without any results.. any help / tips would be appreciated..


heres what I did:

Code: Select all

<?php
$entry_a=($entries[0][entrymsg]);
trim($entry_a);
print($entry_a);
print("hello");
?>

gives output of

Code: Select all

here goes the first message.
hello
there is obviously something there that puts the hello on a separate line...

if anyone knows how to get rid of that please let me know..



Background:


why?

well.. I need to use my php array data in a javascript array and the line break really messes things up...

see example below:

Code: Select all

nam&#1111;9] = "here is the first message
";
nam&#1111;10] = "hello";
gives javascript errors.



the text file itself where the php array feeds from has all the elements on a separate lines.


Trollll helped me write code to put my text file contents into an array, which I have also included..


the topic where that was discussed is here:

http://www.devnetwork.net/forums/viewtopic.php?t=10951

and full code for that was

Code: Select all

<?php 

$fileContents = file("blog.txt");
$entries = array();
for ($i = 0; $i < count($fileContents); $i += 5) {
  array_push($entries, array("entrynum" => $fileContents[$i], "entrymsg" => $fileContents[$i + 1], "entrycolor" => $fileContents[$i + 2], "entrydate" => $fileContents[$i + 3]));
}

?>



my data entry is done via forms and a php form handler..

code for that is:

Code: Select all

&lt;?php
@extract($_POST);
if(is_writable('blog.txt'))
&#123;
$fp = fopen('blog.txt','a');
$content = "\n$entrynum\n$entrymsg\n$category\n$entrydate\n";


fwrite($fp,$content);
fclose($fp);
echo'Successfully written entry';
&#125;
else
&#123;
echo'File is not writable';
&#125;
?&gt;

Posted: Thu Jul 24, 2003 4:50 am
by Fredix
have you tried to use echo in stead of print in

Code: Select all

<?php
$entry_a=($entries[0][entrymsg]);
trim($entry_a);
print($entry_a);
print("hello"); 
?>

Posted: Thu Jul 24, 2003 4:53 am
by oculasm
sure have, Ive tried echo as well.. same result. 8O

Posted: Thu Jul 24, 2003 5:41 am
by marc
file("blog.txt");
the file() function reads a file into an array of strings but the \r\n are left at the end of each line so your $fileContents[$i] content an \r\n

Note: if working on unix like system only a \n is added at each line end

So you have to put off the 2 caracters \r\n ( or only \n) if you want what you expect ...

you can be sure of that by using the strlen() function on each line and you'll discover that the length is 2 (or 1) more than the line characters

have fun

Posted: Thu Jul 24, 2003 6:05 am
by oculasm
hmm.. I follow you..
and thanks for replying,

but how do I cut off the extra charcters? thats the bit Im having trouble with.

Im still quite a newbie.. :oops:

Posted: Thu Jul 24, 2003 6:29 am
by pootergeist
$entry_a = str_replace('
','', trim($entry_a));

and yes, you do want the hardcoded linebreak there.

Posted: Thu Jul 24, 2003 9:02 am
by oculasm
pootergeist.. you're a genius..
it worked!

thanks very much.

:D

-i

Posted: Thu Jul 24, 2003 9:06 am
by oculasm
clever avatar going there :P