I need to perform a number of replacements on a string: remove non-alphanumeric characters, remove duplicate spaces, and capitalize first characters of individual words. Is it a good practice to create a single regexp expression for all 3 tasks, or will performance take a hit if I split this up in 3 separate preg_replace commands?
I need to perform a number of replacements on a string: remove non-alphanumeric characters,
Share
Use two regexes for the first two and then use ucwords. The first two operations aren’t suited to be combined and they are fundamentally different (one replaces with nothing, one replaces with a single character of the match). Then, use
ucwordsto avoid using another regex.Demo