문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top