line break stripping in php for javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

line break stripping in php for javascript

Post 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;
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post 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"); 
?>
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

sure have, Ive tried echo as well.. same result. 8O
marc
Forum Newbie
Posts: 1
Joined: Thu Jul 24, 2003 5:41 am

Post 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
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post 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:
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

$entry_a = str_replace('
','', trim($entry_a));

and yes, you do want the hardcoded linebreak there.
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

pootergeist.. you're a genius..
it worked!

thanks very much.

:D

-i
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

clever avatar going there :P
Post Reply