huddds
huddds

Reputation: 1045

preg_match and remove multiple characters in string?

Hi I'm using php to program my site and I've been reading loads about preg_match and trying lots of examples. What I'm trying to do is something like below...

 if(preg_match('These characters'), $myVariable, matches)){

      Find and remove found characters from $myVariable;

 }

I'm pretty sure this is obvious to php experts but it's had me stuck after hours of trying and reading.

Thanks in advance

Upvotes: 2

Views: 4783

Answers (2)

Aust
Aust

Reputation: 11632

It sounds like you should be using preg_replace. If you wanted to remove all y's and o's for example you would do this:

$string = 'hey you guys!';
$ans = preg_replace('/[yo]/','',$string);
print_r($ans); //outputs 'he u gus!'

Whatever characters you want to remove, just put them between the brackets [...]

Upvotes: 3

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

You don't need to check for a match before doing a replace. It's like if you were to do:

str_replace("A","B","ZZZZZZZ");

It just won't replace anything. Same goes for preg_replace: If there is no match, it just does nothing.

Upvotes: 4

Related Questions