Page 1 of 1

string replacement

Posted: Fri Dec 02, 2005 5:51 am
by Pain
I have this problem with string replacement... I have a string and I want to leave some symbols in it and all the other symbols replace with "-"... I know that there are functions, that replace all the given symbols, but I need the function to replace all the "not given" symblos. Is there a function like this?

Re: string replacement

Posted: Fri Dec 02, 2005 6:08 am
by foobar
Sounds like you need preg_replace(). Without any code it'll be hard to provide any personalised help, though.

Re: string replacement

Posted: Fri Dec 02, 2005 7:03 am
by Pain
foobar wrote:Sounds like you need preg_replace(). Without any code it'll be hard to provide any personalised help, though.
I think it isnt the right one... I will try to give an example... I have a string and want it to contain only symbols "a", "b" and "-"... So what can I do to replace all other symbols with "-"?

Posted: Fri Dec 02, 2005 7:06 am
by mickd
str_replace should do the trick

example:

Code: Select all

$replaced_string = str_replace(array("&", "%", "#"), "-", $string);

Posted: Fri Dec 02, 2005 7:07 am
by Jenk

Code: Select all

$string = preg_replace('/[^ab-]{1}/', '-', $string);

Posted: Fri Dec 02, 2005 7:09 am
by BDKR
Why is preg_replace() not correct? You know something we don't? :)

There are no other builtin functions that can accomplish this for you. preg_replace() is your man. Your only other option would be to iterate over the string and test each character as you go, replacing as needed.

Cheers

Posted: Fri Dec 02, 2005 7:11 am
by BDKR
mickd wrote:str_replace should do the trick

example:

Code: Select all

$replaced_string = str_replace(array("&", "%", "#"), "-", $string);
Based on his description of the problem, str_replace() is going to need a search array that's much larger then what you provided. It's not practical by comparison to preg_replace().

Cheers

Posted: Fri Dec 02, 2005 7:13 am
by mickd
BDKR wrote:
Based on his description of the problem, str_replace() is going to need a search array that's much larger then what you provided. It's not practical by comparison to preg_replace().

Cheers
ah yeah, only realised afterwards that he wanted to keep so few. preg_replace would be better in that case :wink:

Posted: Fri Dec 02, 2005 7:18 am
by Pain
thanks, I just could not find how I could replace the "incorrest" symbols, but now I know and my wisdom has grown :D

Posted: Fri Dec 02, 2005 7:28 am
by foobar
Pain wrote:thanks, I just could not find how I could replace the "incorrest" symbols, but now I know and my wisdom has grown :D
Example:

Code: Select all

<?php

$input = 'I love apples! 2me1three. <some smurf />';

$bad_boys = '/[\w]+/'; //bad "symbols" are non perl-words
$good_guy = '-';
$string = preg_replace($bad_boys, $good_guy, $input);

echo $string;

$bad_boys = '/[^a-zA-Z0-9]+/'; //non alphanumeric characthers (^ is the negation of everything within [])
$good_guy = '-';
$string = preg_replace($bad_boys, $good_guy, $input);

echo $string;

$bad_boys = '/[\^°@~+#?\]\[\(\)\$\!]+/'; //a bunch of non-alphanumeric characters
$good_guy = '-';
$string = preg_replace($bad_boys, $good_guy, $input);

echo $string;

?>