简体   繁体   中英

Remove characters in PHP using regular expressions

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)
...

尝试这个:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM