Home > Backend Development > PHP Problem > How to replace content between characters in php

How to replace content between characters in php

藏色散人
Release: 2023-03-06 06:48:02
Original
2931 people have browsed it

php method to replace the content between characters: first define a string; then find the position of the two specified strings; then use the substr function to delete the middle part; and finally replace the content.

How to replace content between characters in php

Recommended: "PHP Video Tutorial"

It is best not to use regular expressions for this type of replacement, because The only thing you search for does not need to be used, and the replacement text is too large and the efficiency is too low.

The method is to find the positions of these two special strings, and then use substr to delete the middle part. Example code:

$str='....你要处理的字符串.....';
$s1='...开始字符串...';
$s2='...结束字符串...';
$i1=strpos($str,$s1);//开始位置
$i2=strpos($str,$s2);//结束位置
if ($i1!==false && $i2!==false)//找到
$str=substr($str,0,$i1-1) . substr($str,$i2+strlen($s2));
Copy after login

The above is the detailed content of How to replace content between characters in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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