0

Possible Duplicate:
Remove Text Between Parentheses PHP

I have a query string such as

string="this is my string attributeone(values) attributetwo(values,valuestwo) more string"

How can I write a PHP regex to retrieve "this is my string more string" There could be different number of attributes at any place in the sentence and different number of values within them.

Community
  • 1
  • 1
user391986
  • 29,536
  • 39
  • 126
  • 205
  • 1
    replace out anything that matches `attribute.+?\(.*?\)` – Asad Saeeduddin Oct 24 '12 at 19:19
  • will the attributes always be in the same format? have the same names ie attribute???( some, random, attrib, values). Will there be parens the string that are not part of the attrib/values? – Doon Oct 24 '12 at 19:19
  • The attribute names can be totally different it could be category(sports) submitted(yesterday,today). – user391986 Oct 24 '12 at 19:23

1 Answers1

3
$str = preg_replace('/[a-z]+\(.*?\) ?/', '', $input);

or

$str = preg_replace('/[a-z]+\([^)]*\) ?/', '', $input);

This assumes that your attribute names only consist of lower-case letters, and that your values can never contain the ) character. Also note that an attribute at the very end of your string might leave you with a trailing space.

Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • thanks! how about this? /$attribute\([^)]+\)/ – user391986 Oct 24 '12 at 19:28
  • I assume you want to put this inside double quotes and `$attribute` has one specific attribute name you are looking for? then yes, except for the fact that you need to escape your parentheses with double backslashes: `"/$attribute\\([^)]+\\)/"` ... however this does not really match what you described in the question – Martin Ender Oct 24 '12 at 19:30
  • 1
    +1 But instead of the lazy-dot-star, I'd use the more precise (and efficient) expression: `\([^()]*\)` – ridgerunner Oct 24 '12 at 20:13