Re: reg. expressions

From: Date: Wed, 13 Oct 2004 16:48:56 +0000
Subject: Re: reg. expressions
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-199393@lists.php.net to get a copy of this message
Matt M. wrote:
on one place I have string "something (something_2)" I have to take out of the string everything in brackets and brackets as well. probably I will need to use Regular Expression Functions but I'm really bad with them :)
$string = "something (something_2)"; $pattern = "/\(.*\)/"; $replacement = ""; echo preg_replace($pattern, $replacement, $string);
Just note that if there are two "bracketed" patterns in a string, this will have issues. It may or may not be an issue for you, though, depending up on your data. As an alternative: $string = "something (something_2) something (something_4)"; $pattern = '/\([^)]\)/'; //or $pattern = '/\(.*\)/U'; $replacement = ''; echo preg_replace($pattern, $replacement, $string); Also, if there could be line breaks within the "bracketed" text, you'll need extra modifiers... it all just depends upon your data. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com

« previous php.general (#199393) next »