0

I have a MySQL table column with a lot of rows.

Below is a sample of the data format:

TEST-DATA (ID:123)

How can I remove the part (ID:123), using PHP regular expressions?

Please note that (ID:123) contains different numbers for each row in the table column.

TESTDATA2(ID:1)
DATAAGAIN(ID:78)
MOREDATA(ID:45)
...
3
  • are you saving the id data as a text in your database? Commented Aug 21, 2012 at 21:43
  • no. i dont need the ID and everything in the brackets. i just need the text that is outside the brackets. Commented Aug 21, 2012 at 21:50
  • eg for TEST-DATA(ID:123) i need to remain with TEST-DATA only. Commented Aug 21, 2012 at 21:51

2 Answers 2

4

You can use preg_replace().

preg_replace — Perform a regular expression search and replace

Example:

echo preg_replace("/\([^\)]+\)/", "", $value);

Expression breakdown:

/  - Opening delimiter 
\( - Match an opening parenthesis 
[^\)]+ - Match one or more characters that are not a closing parenthesis 
\) - Match a closing parenthesis
/  - Closing delimiter

Replace $value with the variable for your column value.

Sign up to request clarification or add additional context in comments.

3 Comments

I would also include \s* before the opening parenthesis.
you mean like this : echo preg_replace("\s*/([^)]+)/", "", $value); ?
thanks for the tip Aleck. The \s* means ignore spaces at the beginning, right?
4

Try this:

$string = preg_replace( '/\(ID:[0-9]+\)/', '', $string );

2 Comments

I would also include \s* before the opening parenthesis.
Gentlemen, you are truly the best people on the planet!! It worked perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.