Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 07-19-2007, 03:03 PM
Quarterbore Quarterbore is offline
 
Join Date: Mar 2005
Location: Valley Forge PA
Posts: 538
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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!
Reply With Quote
  #2  
Old 07-19-2007, 03:15 PM
nico_swd's Avatar
nico_swd nico_swd is offline
 
Join Date: Dec 2005
Location: Spain
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It's \n and not /n... (backslash). And it should go between double quotes.
Reply With Quote
  #3  
Old 07-19-2007, 03:46 PM
Quarterbore Quarterbore is offline
 
Join Date: Mar 2005
Location: Valley Forge PA
Posts: 538
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by nico_swd View Post
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

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:
Reply With Quote
  #4  
Old 07-19-2007, 03:56 PM
nico_swd's Avatar
nico_swd nico_swd is offline
 
Join Date: Dec 2005
Location: Spain
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Remove the "carriage return" characters as well.

PHP Code:
$test1ereg_replace("[\r\n]+"'YEA'$myvalues); 
Reply With Quote
  #5  
Old 07-19-2007, 04:02 PM
Quarterbore Quarterbore is offline
 
Join Date: Mar 2005
Location: Valley Forge PA
Posts: 538
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by nico_swd View Post
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!
Reply With Quote
  #6  
Old 07-19-2007, 04:08 PM
nico_swd's Avatar
nico_swd nico_swd is offline
 
Join Date: Dec 2005
Location: Spain
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You're welcome. I'm happy to help.
Reply With Quote
  #7  
Old 07-20-2007, 12:39 PM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You should use the Perl functions (preg) for regex rather than the PHP ones (ereg).
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:48 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.07015 seconds
  • Memory Usage 2,239KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_php
  • (5)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete