How to delete a string in php regular

藏色散人
Release: 2023-03-06 20:26:01
Original
2771 people have browsed it

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); 
 
?>
Copy after login

Run results

a、b、c科d
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template