在PHP中要替换串中指定字符我们一般会一次全部替换,如str_replace函数,但有时只想替换第一次出现的,像文章的关键词替换了,这个如果有100个不可能出现100次啊,我只想限制几次了,下面我来给各位介绍实现方法。
例:$str='这是字符串我只替换ABC一次后面的ABC我不替换了,有没有办法实现.';
把第一个abc替换成xyz,由于要替换的字符串是固定的,很多人想到了用str_replace()函数,看看这个函数的使用是不是我们要的.
str_replace( mixed $search,mixed $replace,mixed $subject [,int &$count ] )
不小心还真以为是我们想要的呢,最后那个参数是返回替换发生的总次数,它是一个引用变量,而不是我要想要的指定它将替换几次,所以用str_replace()是不行的.
preg_replace()是可以实现的,可惜用了正则,代码如下:
$str=preg_replace('/abc/','abc',$str,1); echo $str;
例:显示email为从@前2位(含)开始向前隐藏4位,代码如下:
- @H_502_27@function@H_502_27@ show_email_2($string@H_502_27@){
- @H_502_27@ $first@H_502_27@ = strpos@H_502_27@($string@H_502_27@, '@'@H_502_27@);
- @H_502_27@ //var_dump($first);@H_502_27@
- @H_502_27@ if@H_502_27@($first@H_502_27@==1){
- @H_502_27@ $string@H_502_27@ = '****'@H_502_27@.$string@H_502_27@;
- @H_502_27@ }
- @H_502_27@ if@H_502_27@($first@H_502_27@>1 && $first@H_502_27@<=5){
- @H_502_27@ $string@H_502_27@ = substr_replace($string@H_502_27@,'****'@H_502_27@,$first@H_502_27@-1);
- @H_502_27@ }
- @H_502_27@ if@H_502_27@($first@H_502_27@>5){
- @H_502_27@ $string@H_502_27@ = substr_replace($string@H_502_27@,$first@H_502_27@-5,4);
- @H_502_27@ }
- @H_502_27@
- @H_502_27@ var_dump($string@H_502_27@);
- @H_502_27@ return@H_502_27@ $string@H_502_27@;
- @H_502_27@ }
- @H_502_27@ //show_email_2('22@163.com'); //输出-->****2@163.com@H_502_27@
- @H_502_27@ //show_email_2('22@22.com'); //输出-->****2@22.com@H_502_27@
- @H_502_27@ show_email_2('6123456@163.com'@H_502_27@); //输出-->61****6@163.com@H_502_27@
有没有不用正则的,嗯可以这样
- @H_502_27@$replace@H_502_27@='xyz'@H_502_27@;
- @H_502_27@if@H_502_27@(($position@H_502_27@=strpos@H_502_27@($str@H_502_27@,$replace@H_502_27@))!==false){
- @H_502_27@ $leng@H_502_27@=strlen@H_502_27@($replace@H_502_27@);
- @H_502_27@ $str@H_502_27@=substr_replace($str@H_502_27@,'abc'@H_502_27@,$position@H_502_27@,$leng@H_502_27@);
- @H_502_27@}
- @H_502_27@echo@H_502_27@ $str@H_502_27@;
- @H_502_27@@H_502_27@<?PHP
- @H_502_27@/*
- @H_502_27@ * $text是输入的文本;
- @H_502_27@ * $word是原来的字符串;
- @H_502_27@ * $cword是需要替换成为的字符串;
- @H_502_27@ * $pos是指$word在$text中第N次出现的位置,从1开始算起
- @H_502_27@ * */@H_502_27@
- @H_502_27@function@H_502_27@ changeNstr($text@H_502_27@,$word@H_502_27@,$cword@H_502_27@,$pos@H_502_27@=1){
- @H_502_27@$text_array@H_502_27@=explode@H_502_27@($word@H_502_27@,$text@H_502_27@);
- @H_502_27@$num@H_502_27@=count@H_502_27@($text_array@H_502_27@)-1;
- @H_502_27@if@H_502_27@($pos@H_502_27@>$num@H_502_27@){
- @H_502_27@return@H_502_27@ "the number is too big!or can not find the $word"@H_502_27@;
- @H_502_27@}
- @H_502_27@$result_str@H_502_27@=''@H_502_27@;
- @H_502_27@for@H_502_27@($i@H_502_27@=0;$i@H_502_27@<=$num@H_502_27@;$i@H_502_27@++){
- @H_502_27@if@H_502_27@($i@H_502_27@==$pos@H_502_27@-1){
- @H_502_27@$result_str@H_502_27@.=$text_array@H_502_27@[$i@H_502_27@].$cword@H_502_27@;
- @H_502_27@}else@H_502_27@{
- @H_502_27@$result_str@H_502_27@.=$text_array@H_502_27@[$i@H_502_27@].$word@H_502_27@;}
- @H_502_27@}
- @H_502_27@return@H_502_27@ rtrim($result_str@H_502_27@,$word@H_502_27@);
- @H_502_27@}
- @H_502_27@$text@H_502_27@='hello world hello pig hello cat hello dog hello small boy'@H_502_27@;
- @H_502_27@$word@H_502_27@='hello'@H_502_27@;
- @H_502_27@$cword@H_502_27@='good-bye'@H_502_27@;
- @H_502_27@echo@H_502_27@ changeNstr($text@H_502_27@,3);
- @H_502_27@//输出:hello world hello pig good-bye cat hello dog hello small boy @H_502_27@
- @H_502_27@?>
实例都很好理解,如果你不想深入了解我们直接使用str_replace即可实例了.