replacing characters in string by white spaces

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sploit
Forum Newbie
Posts: 6
Joined: Wed Dec 04, 2002 6:04 am

replacing characters in string by white spaces

Post 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.
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

sploit,
Try the non breaking space like this.



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

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




John M
User avatar
sploit
Forum Newbie
Posts: 6
Joined: Wed Dec 04, 2002 6:04 am

Post by sploit »

thanks Johnm!
Peter A. Shushpanov
Forum Newbie
Posts: 4
Joined: Mon Dec 23, 2002 3:38 am
Location: Russia
Contact:

Post 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);
Post Reply