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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.