Home >Backend Development >PHP Problem >How to delete a string in php regular
php method to delete strings regularly: first create a PHP sample file; then define a set of strings; finally, use the regular expression "$regex="/(Department | Office)(?=(,| $))/";" to delete the specified string.
Recommended: "PHP Video Tutorial"
Specific questions:
Now I have a bunch of strings such as department a, room b, department c and room d. I want to delete the strings ending with department and room. After deleting department c and room d, it will be c department d. How to write responsibility?
Solution:
|room
php regular expression /(department|room)(?=(,|$) )/
The php program to delete departments and offices written according to your requirements is as follows
<?php $str="a科、b室、c科d室"; $regex="/(科|室)(?=(、|$))/"; echo preg_replace($regex,"",$str); ?>
Run results
a、b、c科d
The above is the detailed content of How to delete a string in php regular. For more information, please follow other related articles on the PHP Chinese website!