views:

197

answers:

4

hi,

similiar like this example, http://stackoverflow.com/questions/1336672/php-remove-brackets-contents-from-a-string i have no idea to replace

$str = '(ABC)some text'

into

$str = 'ABC';

currently use $str = preg_replace('/(.)/','',$str); but not works. how to fix this?

+2  A: 

Instead of preg_replace, I would use preg_match:

preg_match('#\(([^)]+)\)#', $str, $m);
echo $m[1];
Carsten Gehling
Realistically, this is a more clear cut case for matching on a string rather than replacing a string.
gnarf
@Carsten Gehling - Welcome to Stack Overflow! Start each line with 4 spaces to format it as code - http://stackoverflow.com/editing-help . Also,there's no need to sign your post.
Kobi
thanks this works!
apis17
A: 

Try this:

$str = preg_replace('/\((.*?)\).*/','\\1',$str);
turbod
thank you. this works :)
apis17
+1  A: 

If you want to use replace, you could use the following:

 $str = "(ABC)some text";
 $str = preg_replace("/^.*\(([^)]*)\).*$/", '$1', $str);

The pattern will match the whole string, and replace it with whatever it found inside the parenthesis

gnarf
this also works fine. :)
apis17
A: 

I'd avoid using regex altogether here. Instead, you could use normal string functions like this: $str = str_replace(array('(',')'),array(),$str);

Savui
thanks user366075, but this not remove *some text* at the end of bracket. only wants string in bracket and remove others.
apis17