Question

I have a list of games and regions in my database and i messed a few up, They are missing the end bracket so most are named like Sonic The Hedgehog (E but the letter E will change to many different regions.

How can i get rid of just the single bracket and letters ? It is always the last part of my string if that helps.

Thanks

Was it helpful?

Solution

Using preg_replace:

$str = preg_replace('/\([^)]$/', '', $str);

This will replace a left parenthesis which is followed by any character other than right parenthesis at the end of an input with empty string.

OTHER TIPS

if it's in all strings you can just cut 3 last chars like this

substr($str, 0, -3);

And even id it's not you still don't have to use regex. You can trim all "rear" character like this

rtrim($str, " (E");

it will remove all trailing spaces, 'E's and left braces. By using trim it will remove the same from the front also.

You'll want to use preg_replace to replace all strings.

$str = preg_replace('/ \(.*$/', '', $str);

That regular expression finds a space, followed by a right-parenthesis, followed by zero or more characters. This works on any of the following possibilities: (, (E, (Eabcdef

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top