vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Stripping line breaks from a string - ereg_replace & str_replace (https://vborg.vbsupport.ru/showthread.php?t=152759)

Quarterbore 07-19-2007 03:03 PM

Stripping line breaks from a string - ereg_replace & str_replace
 
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 Code:

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

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

Outputs the data like this (HTML CODE):

Quote:

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

Quote:

Originally Posted by nico_swd (Post 1295767)
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 Code:

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


echo 
'<br />TEST 1<br />';
$test1ereg_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:

Quote:

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.

PHP Code:

$test1ereg_replace("[\r\n]+"'YEA'$myvalues); 


Quarterbore 07-19-2007 04:02 PM

Quote:

Originally Posted by nico_swd (Post 1295798)
Remove the "carriage return" characters as well.

PHP Code:

$test1ereg_replace("[\r\n]+"'YEA'$myvalues); 



Thank You, that was the problem!

My Test Code:

PHP 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 />';
$test3str_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:

Quote:

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).


All times are GMT. The time now is 09:29 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01080 seconds
  • Memory Usage 1,747KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_php_printable
  • (5)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete