I need a Regular Expression to remove ALL single characters from a string, not just single letters or numbers
The string is:
“A Future Ft Casino Karate Chop ( Prod By Metro )”
it should come out as:
“Future Ft Casino Karate Chop Prod By Metro”
The expression I am using at the moment (in PHP), correctly removes the single ‘A’ but leaves the single ‘(‘ and ‘)’
This is the code I am using:
$string = preg_replace('/\b\w\b\s?/', '', $string);
Try this:
Breakdown:
Actual code:
Note: I’m not familiar with the workings of PHP regex, so the code might need a slight tweak depending on how the actual regex needs declared.
As m.buettner pointed out, there will be a trailing white space here with this code. A trim would be needed to clear it out.
Edit: Arnis Juraga pointed out that this would not clear out multiple single characters
a b cwould filter out tob. If this is an issues use this regex:The
(( ).)*added to the middle will look for any space following by any character 0 or more times. The downside is this will end up with double spaces where a series of single characters were located.Meaning this:
Will become this:
After performing the replacement to get single individual characters, you would need to use the following regex to locate the double spaces, then replace with a single space