
字符串是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






