Currently I’m using
$str = preg_replace("/\([^\)]+\)/", "", $str); // Remove () and Content
$str = preg_replace("/\[[^\)]+\]/", "", $str); // Remove [] and Content
$str = preg_replace("/\{[^\)]+\}/", "", $str); // Remove {} and Content
$str = preg_replace("/[^a-zA-Z0-9]/", "", $str); // Remove all non-alphanumeric characters
I was wondering if there was a way to combine these into a single regex statement
You can use
|to combine those:But in your case, I would usually just go through the string and parse it manually.
With “state variables” (
$paren_open,$brace_open,$curly_open) to know if you ignore the char or not.Even if it doesn’t seem fast (because you have to go through every character), it’s much quicker than regexes, because the regex will do something way more complicated.
Resources: