onedumbcoder Posted April 2, 2009 Share Posted April 2, 2009 I am trying to replace '\n' with '\n |' but when I do this preg_replace('[\n]', '\n | ', $messeage) the results is a string that shows the '\n' for example if i have Hi I am someone I want it to show up as |Hi I am |someone instead i am getting \n |Hi I am\n |someone I cant just do preg_replace('[\n]', '| ', $messeage) because then I will lose the new lines Quote Link to comment Share on other sites More sharing options...
premiso Posted April 3, 2009 Share Posted April 3, 2009 $string = "Testing\nand testing again\n"; $string = str_replace("\n", "\n |", $string); Use str_replace for simple replacements. Quote Link to comment Share on other sites More sharing options...
Maq Posted April 3, 2009 Share Posted April 3, 2009 Yeah, if you don't need regex then just use what premiso suggested. It should be a bit faster as well. Quote Link to comment Share on other sites More sharing options...
redarrow Posted April 3, 2009 Share Posted April 3, 2009 example <?php $word="\n hi there \n i am redarrow"; $test=str_replace("\n","|",$word); echo $test; ?> Quote Link to comment Share on other sites More sharing options...
Maq Posted April 3, 2009 Share Posted April 3, 2009 Is there an echo in here? Quote Link to comment Share on other sites More sharing options...
redarrow Posted April 3, 2009 Share Posted April 3, 2009 I was thinking and i slipped sorry. why over kill your code, read all the post. Quote Link to comment Share on other sites More sharing options...
onedumbcoder Posted April 3, 2009 Author Share Posted April 3, 2009 Thanks guys but i figured out to do it a different way, and it works great. I took this approach: preg_replace('[\n]', chr(13).'| ', $message) Quote Link to comment Share on other sites More sharing options...
Philip Posted April 3, 2009 Share Posted April 3, 2009 I think someone (*caugh*Maq*cough*) is just jealous they aren't a top 10 poster. As the others said, it's better to use str_replace, it's going to be a bit faster since it doesnt have to parse the regex Quote Link to comment Share on other sites More sharing options...
Maq Posted April 3, 2009 Share Posted April 3, 2009 I was thinking and i slipped sorry. Hehe, I was just kidding buddy! I think someone (*caugh*Maq*cough*) is just jealous they aren't a top 10 poster. Aww man, you got me, figured me out... Did you have to go there? Quote Link to comment Share on other sites More sharing options...
redarrow Posted April 3, 2009 Share Posted April 3, 2009 i no maq mate. can you show a statistic in speed please, for str_replace and regex. Quote Link to comment Share on other sites More sharing options...
Maq Posted April 3, 2009 Share Posted April 3, 2009 Don't know how accurate it is: http://www.php-scripts.com/php_diary/011303.php3 You could also do one yourself just to be sure. Quote Link to comment Share on other sites More sharing options...
Philip Posted April 3, 2009 Share Posted April 3, 2009 A quick and dirty test on my server, 100,000 tests of each: for($i=0;$i<100000;$i++) $x = str_replace(' x ','', $string); And for($i=0;$i<100000;$i++) $x = preg_replace('~ x ~','', $string); Showed 0.157774925232 seconds and 0.217579841614 seconds, respectively. Tried about 10 times, about the same results each time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.