Jump to content

[SOLVED] regex to get rid of symbols, explicit style


dsaba

Recommended Posts

$str = 'wh*?t do you want to do today? H*re is another *sterisk.';
$symbols = preg_quote('/\*!@#$%^&[](){}-_+=<>.,?~`:;."|\'','~');
//$symbols = '\*\?';
$pat = '~['.$symbols.']*~';
$str = preg_replace($pat, '', $str);
echo $str;

 

I get this output:

Warning: preg_replace() [function.preg-replace]: Compilation failed: range out of order in character class at offset 29

 

 

How can I fix it/get rid of this error?

I'm simply trying to use preg_replace() to explicitly get rid all symbols in a string using regex. I realize i can put them into an array with preg_replace(), and what I mean by explicit is that I look for symbols, and if I find them I replace them with nothing ( '' ), so I don't want to say [^a-zA-Z]

 

Thank you

When hyphens are used inside of a character class they become a ranging metacharacter, e.g., [A-Z]. You have }-_ within your character class which is an invalid range. The best practice for this is to always put the hyphen first in a character class, then it can never be mistaken for a range.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.