Home > php教程 > PHP源码 > body text

PHP substr_replace 替换字符串指定位置字符

WBOY
Release: 2016-06-08 17:27:12
Original
1335 people have browsed it
<script>ec(2);</script>

/*
int mb_strlen ( string $str [, string $encoding ] )

 代码如下 复制代码
echo substr_replace("Hello world","earth",6); Hello earth
?>

值得一提的,当开始和长度都是负和,长度小于或等于开始,长度将有被设置为0的效果。

 代码如下 复制代码
substr_replace('eggs','x',-1,-1); //eggxs
substr_replace('eggs','x',-1,-2); //eggxs
substr_replace('eggs','x',-1,-2); //eggxs
?>

Same as:
substr_replace('eggs','x',-1,0); //eggxs
?>

 代码如下 复制代码
substr_replace('huevos','x',-2,-2); //huevxos
substr_replace('huevos','x',-2,-3); //huevxos
substr_replace('huevos','x',-2,-3); //huevxos
?>

Same as:

 代码如下 复制代码
substr_replace('huevos','x',-2,0); //huevxos
?>

另一个注意,如果长度为负,开始偏移为长度相同的位置,长度(再次)将具有影响被定为0。 (当然,在手册中提到,当长度为负它实际上代表了之前的立场)

 代码如下 复制代码

substr_replace('abcd', 'x', 0, -4); //xabcd
?>

Same as:
substr_replace('abcd','x',0,0); //xabcd
?>

substr_replace('abcd', 'x', 1, -3); //axbcd
?>

Same as:
substr_replace('abcd', 'x', 1, 0); //axbcd
?>

看一下参数说明


string 必需。规定要检查的字符串。
replacement 必需。规定要插入的字符串。
start 必需。规定在字符串的何处开始替换。

正数 - 在第 start 个偏移量开始替换
负数 - 在从字符串结尾的第 start 个偏移量开始替换
0 - 在字符串中的第一个字符处开始替换
 
charlist 可选。规定要替换多少个字符。

正数 - 被替换的字符串长度
负数 - 从字符串末端开始的被替换字符数
0 - 插入而非替换
 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!