PDA

View Full Version : Stripping line breaks from a string - ereg_replace & str_replace


Quarterbore
07-19-2007, 03:03 PM
I have a problem with a script in that the script saved line breaks between values that were saved. Well, the script was written that way for another purpose but now it is causing me issues...

I know I can take normal text like this and remove the /n's

$myvalues = 'cars/ntrucks/nautomibiles';

Using code like this:

$myvalues = ereg_replace('/n', '', $myvalues);

HOWEVER, I do not know how to remove LINE BREAKS which I assume would be \n in the data?

For example:

<?php
$myvalues = 'cars
trucks
automibiles';
echo $myvalues . '<br />';

$myvalues = ereg_replace('/n', '', $myvalues);
echo $myvalues . '<br />';
?>

Outputs the data like this (HTML CODE):

cars
trucks
automibiles<br />cars
trucks
automibiles<br />

Can someone help be strip these line breaks out of my string?

EDIT to change the title to help someone else that may search this!

nico_swd
07-19-2007, 03:15 PM
It's \n and not /n... (backslash). And it should go between double quotes.

Quarterbore
07-19-2007, 03:46 PM
It's \n and not /n... (backslash). And it should go between double quotes.

Well, I am closer but the darned line break remains in the code :confused:

I have also been trying to do the same thing with "str_replace" so I used that in my test code too.

Here is my test code:


<?php
$myvalues = 'cars
trucks
automobiles';
echo 'DATA IN<br />';
echo nl2br($myvalues) . '<br />';


echo '<br />TEST 1<br />';
$test1= ereg_replace("\n", 'YEA', $myvalues);
echo nl2br($test1) . '<br />';

echo '<br />TEST 2<br />';
$test2 = str_replace("\n", "GO", $myvalues);
echo nl2br($test2) . '<br />';
?>


Here is the output:


DATA IN
cars
trucks
automobiles

TEST 1
cars
YEAtrucks
YEAautomobiles

TEST 2
cars
GOtrucks
GOautomobiles


Perhaps I am going about this wrong? I just want to kill those nastly little line breaks :erm:

nico_swd
07-19-2007, 03:56 PM
Remove the "carriage return" characters as well.


$test1= ereg_replace("[\r\n]+", 'YEA', $myvalues);

Quarterbore
07-19-2007, 04:02 PM
Remove the "carriage return" characters as well.


$test1= ereg_replace("[\r\n]+", 'YEA', $myvalues);



Thank You, that was the problem!

My Test Code:


<?php
$myvalues = 'cars
trucks
automobiles';
echo 'DATA IN<br />';
echo nl2br($myvalues) . '<br />';


echo '<br />TEST 2<br />';
$test2 = str_replace("\n", "GO", $myvalues);
echo nl2br($test2) . '<br />';

echo '<br />TEST 3<br />';
$test3= str_replace("\r\n", "NO", $myvalues);
echo nl2br($test3) . '<br />';

echo '<br />TEST 4<br />';
$test4 = str_replace("\r\n", "", $myvalues);
echo nl2br($test4) . '<br />';

?>


OUTPUT:


DATA IN
cars
trucks
automobiles

TEST 2
cars
GOtrucks
GOautomobiles

TEST 3
carsNOtrucksNOautomobiles

TEST 4
carstrucksautomobiles


That is exactly what I needed! This is the second time you saved me, I really appreciate your help!

nico_swd
07-19-2007, 04:08 PM
You're welcome. I'm happy to help. :)

Dismounted
07-20-2007, 12:39 PM
You should use the Perl functions (preg) for regex rather than the PHP ones (ereg).