Page 1 of 1

replacing characters in string by white spaces

Posted: Thu Dec 19, 2002 5:09 am
by sploit

Code: Select all

<?php
$pattern = "\+"; 
$replacement = "  "; 

echo ereg_replace($pattern, $replacement, "S++PL+++OIT"); 
?>
why doesnt this print
S[space][space]PL[space][space][space]OIT
It just replaces all the + signs by _one_ space, and prints:
S PL OIT
:?:

I need to replace multiple occurrences of + in a string by spaces.

Posted: Thu Dec 19, 2002 7:13 am
by Johnm
sploit,
Try the non breaking space like this.



$pattern = "\+";
$replacement = "&nbsp;&nbsp;";

echo ereg_replace($pattern, $replacement, "S++PL+++OIT");




John M

Posted: Fri Dec 20, 2002 2:19 am
by sploit
thanks Johnm!

Posted: Mon Dec 23, 2002 3:47 am
by Peter A. Shushpanov
sploit wrote:thanks Johnm!
Don't use Regular Expressions everythere: they are rather slow, string functions not.

Code: Select all

$replaced = str_replace("+", "&nbsp;", $your_string);