I have used preg_match to check whether a string matches a given regular expression, but not to parse out the invalid characters. This is what I want to do, in a function named toSearchableString($string):
Passed original string: Where'd You Go (feat. Holly Brook and Jonah Matranga)
Returned sanitized string: Whered You Go feat Holly Brook and Jonah Matranga
If we have to replace the characters that are invalid with anything, a space is preferred (or an empty string to simply remove them).
This is the REGEX I want it matched against: a-zA-Z0-9 and a space
Thanks!
Use
preg_replace()with a pattern that matches invalid characters as the first argument, an empty string as the second argument, and your original string as the third argument.