Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

mySQL: Multiple Replace

I use the following PHP / mySQL code to search for matches after removing spaces.  Now I want to remove multiple characters, not just spaces:

!+$*-.?,';_()

I can do this in PHP like this:
      str_replace(array('!', '+', '$', '*', '-', '.', '?', ',', \''', ';', '_', '(', ')' )','',$term)

But how can I do this with mySQL?
$query = "select `name` from `organizations` where replace(`name`, ' ', '') LIKE '%". str_replace(' ','',$term) . "%'";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you might consider to create a second column clean_name in your table, and have the "cleaned" value there, so you don't need to do this all the time, and you could even profit from an index on the field.-..
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
preg_replace( pattern, replacement, subject ,[limit])

preg_replace("/!+$*-.?,';_()/", '',STRING)

Ref: http://www.webcheatsheet.com/php/regular_expressions.php#replace