I'll-Be-Back
I'll-Be-Back

Reputation: 10838

remove string between brackets

In PHP, how to remove string between Active/End Date parenthesis?

For example:

$String1  = "Text Text - (Active Date: 12-03-2011 , End Date:12-03-2013)";
$String2  = "Free Free Text Text(2000 min)-Ret - (Active Date: 12-03-2011 , End Date: )";

I want $String1 to replace to: "Text Text"

and $String2 to "Free Free Text Text(2000 min)-Ret"

Upvotes: 0

Views: 1095

Answers (3)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57690

This will do.

$pattern = '/\s*\-\s*\(\s*Active[^\)]+\)/';
$String1 = preg_replace($pattern, '', $String1);
$String2 = preg_replace($pattern, '', $String2);

Upvotes: 3

miqbal
miqbal

Reputation: 2227

substr( $string, 0, strpos($string," - (") );

Edit: After, you may use trim() for stripping whitespaces from begin and end of the string.

Upvotes: 0

Paul Bain
Paul Bain

Reputation: 4397

You'll want to use something like:

preg_replace('/\(Active.*?\)/','(text Text)',$String1);

Note that the parenthesis need to be escaped with \ so they are interpreted as literals, not as control characters.

Upvotes: 0

Related Questions