Home >Backend Development >PHP Problem >How to delete a string in php regular

How to delete a string in php regular

藏色散人
藏色散人Original
2020-10-30 09:22:502800browse

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.

How to delete a string in php regular

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn