简体   繁体   English

使用正则表达式删除PHP中的字符

[英]Remove characters in PHP using regular expressions

I have a MySQL table column with a lot of rows. 我有一个包含很多行的MySQL表列。

Below is a sample of the data format: 以下是数据格式的示例:

TEST-DATA (ID:123)

How can I remove the part (ID:123) , using PHP regular expressions? 如何使用PHP正则表达式删除零件(ID:123)

Please note that (ID:123) contains different numbers for each row in the table column. 请注意(ID:123)在表格列的每一行包含不同的数字。

TESTDATA2(ID:1)
DATAAGAIN(ID:78)
MOREDATA(ID:45)
...

尝试这个:

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

You can use preg_replace() . 您可以使用preg_replace()

preg_replace — Perform a regular expression search and replace preg_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. $value替换为您的列值的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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