php str_replace函数,可限制替换个数
发布时间:2022-06-16 09:51:35 所属栏目:PHP教程 来源:互联网
导读:本文章主要是介绍str_replace函数替换单个字符,与替换数据,以及指定str_replace替换的次数,最后一个替换次数是很有用的特别是在seo优化方面. mixed str_replace(mixed $search , mixed $replace , mixed $subject [,int $count ] ) 函数返回一个字符串或在出
|
本文章主要是介绍str_replace函数替换单个字符,与替换数据,以及指定str_replace替换的次数,最后一个替换次数是很有用的特别是在seo优化方面. mixed str_replace(mixed $search , mixed $replace , mixed $subject [,int &$count ] ) 函数返回一个字符串或在出现问题的所有搜索给定的数组替换值替换,如果你不喜欢需要更换的规则(如正则表达式),你应该始终使用此函数,而不是ereg_replace()或preg_replace函数(),实例代码如下: */ // provides: <body text='black'> $bodytag = str_replace("%body%", "black", "<body text='%body%'>"); // provides: hll wrld f php $vowels = array("a", "e", "i", "o", "u", "a", "e", "i", "o", "u"); $onlyconsonants = str_replace($vowels, "", "hello world of php"); // provides: you should eat pizza, beer, and ice cream every day $phrase = "you should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "beer", "ice cream"); $newphrase = str_replace($healthy, $yummy, $phrase); // provides: 2 $str = str_replace("ll", "", "good golly miss molly!", $count); echo $count; //利用str_replace函数指定替换次数 $array = array( array(0,1,2) ); function keywords( $str,$array ) { $count =0; foreach($array as $v){ if(strstr($str,strtolower($v[0]))!==false){ if( $count <=3 ){ $tos = strtolower($v[0]); $str=preg_replace("/$tos/","<a href=".$v[1]." target=_blank >".$v[2]."</a>",$str,1);//开源软件:phpfensi.com $count++; continue; } } } return $str; } (编辑:南阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

