codeispoetry
codeispoetry

Reputation: 373

php preg_replace: replace every occurrence of element (don't group)

I use this code:

preg_replace('/[^A-ÿ\d _]+/', 'NO' , $mystring]

To find and replace everything that is not alphanumerical, or a space or an underscore, with ***

Only problem is that, if my input is:

tes<<t 

it outputs:

tesNOt

and I would like:

tesNONOt

I would like it replacing EVERY occurrence of the 'wrong' character.

Thanks for your help!

Upvotes: 1

Views: 530

Answers (1)

mickmackusa
mickmackusa

Reputation: 47864

Your pattern is matching "one or more".

You want to match each one.

$mystring='tes<<t';
echo preg_replace('/[^A-ÿ\d _]/', 'NO' , $mystring);
// output: tesNONOt

Demo Link

Upvotes: 1

Related Questions