详解php中的str_replace()(附代码实例)

 2897

607e73a97e4a8.png


字符串是php中比较常用的类型,对字符串的替换也没有数组来的更加方便,那么如何对字符串内的字符进行替换的,本文就带大家一起来看一看如何以利用str_repalce()函数来完成,首先我们来看一看函数的语法:

str_replace    ( mixed $search   , mixed $replace   , mixed $subject   , int $count = ?   )

$search:查找的目标值,可以指定多个目标。

$replace:search 的替换值。

$subject:执行替换的数组或者字符串

$count:可选,如果被指定,它的值将被设置为替换发生的次数。

返回值:该函数返回替换后的数组或者字符串。


代码实例:

1、有必需的三个参数

<?php
$str="ok is great,that is zztuku.com";
$str1=str_replace("is","hehe",$str);
print_r($str1);
?>

输出:

ok hehe great,that hehe zztuku.com


2、有四个参数

<?php
$str="ok is great,that is zztuku.com";
$str1=str_replace("is","hehe",$str,$count);
print_r($str1);
echo "<br>"."总共替换了:".$count;
?>

输出:

ok hehe great,that hehe zztuku.com
总共替换了:2



TAG标签:
本文网址:https://www.zztuku.com/detail-8839.html
站长图库 - 详解php中的str_replace()(附代码实例)
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐

    八个可爱的秋叶矢量素材