1

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.

Example:

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.

1 Answer 1

3

You could use regex to only allow alphanumeric characters.

$string = preg_replace("/[^A-Za-z0-9 ]/", '', $string);
1
  • 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
    Commented Nov 14, 2011 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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