WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to check whether a field has special characters, remove those characters and output (save) the value to another field.

I am creating a user search function that search various fields, but those fields might (or might not) have special characters in them that will return unexpected results, or no results at all. I need to be able to remove any special characters and save/output the clean value to another field, which I will then include in my search function.


Original value:

G-P's Bargain Shop

After removing special characters:

GPs Bargin Shop

Then save new value (GPs Bargain Shop) to another field

Can someone tell me how to achieve this? Or of a better solution for what Im trying to accomplish. Thanks for any help with this.

share|improve this question
up vote 1 down vote accepted

You could use regex to only allow alphanumeric characters.

$string = preg_replace("/[^A-Za-z0-9 ]/", '', $string);
share|improve this answer
thanks. This worked for me, but now Im noticing a problem with the ampersand character (&). I was able to set up a replacement system that will replace "&" with the word "and." The new value saves to database correctly, but when I search the new value Im not able to get and results with my custom query. The ampersand character translate to & in html and I think this has something to do with my issue. – user1893 Nov 14 '11 at 21:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.