教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP 替换字符串中的一些字符方法介绍

PHP 替换字符串中的一些字符方法介绍

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供PHP 替换字符串中的一些字符方法介绍等资源,欢迎您收藏本站,我们将为您提供最新的PHP 替换字符串中的一些字符方法介绍资源
在php中替换字符串我们都会使用到str_replace函数了,此函数还可以使用正则,下面小编来给大家介绍一下替换字符串中的一些字符或替换第一次出现的字符实例。

现在有个需求:字符串A与字符串B,字符串B中包含字符串A,利用字符串A将字符串B中的A替换成其他字符串或删除。

利用PHP函数,str_ireplace() 与 str_replace() 可以做到。

一、str_ireplace(find,replace,string,count) 函数使用一个字符串替换字符串中的另一些字符(该函数对大小写不敏感)。

例如:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1785')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1785>

<?php
     header(“Content-Type: text/html; charset=utf-8"); // 防止中文乱码
    $str_1 = '郭g碗w瓢p盆p';
    $str_2 = '?潘?';
    $str_3 = 'PHP 替换字符串中的一些字符串-郭G碗w瓢p盆P';
                             
    $str = str_ireplace($str_1,$str_2,$str_3);
    echo $str;
    // 输出:PHP 替换字符串中的一些字符串-?潘
?>

 二、str_replace(find,replace,string,count) 函数使用一个字符串替换字符串中的另一些字符(该函数对大小写敏感)。


(参数与描述同 str_ireplace() 函数)

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5287')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5287>

<?php
    header(“Content-Type: text/html; charset=utf-8"); // 防止中文乱码
    $str_1_s = '郭g碗w瓢p盆p';
    $str_1_b = '郭G碗w瓢p盆P';
    $str_2 = '?潘?';
    $str_3 = 'PHP 替换字符串中的一些字符串-郭G碗w瓢p盆P';
             
    $str_s = str_replace($str_1_s,$str_2,$str_3).'<br /><br />';
    $str_b = str_replace($str_1_b,$str_2,$str_3);
    echo $str_s; // 无法查找到,输出原字符串
    echo $str_b; // 被正确替换
    // $str_s 输出:PHP 替换字符串中的一些字符串-郭G碗w瓢p盆P
    // $str_b 输出:PHP 替换字符串中的一些字符串-?潘
?>

上面要替换肯定全部替换了,我如果想只替换第一次出现的字符呢

很多人想到了用str_replace()函数,看看这个函数的使用是不是我们要的
str_replace( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

不小心还真以为是我们想要的呢,最后那个参数是返回替换发生的总次数,它是一个引用变量,而不是我要想要的指定它将替换几次,所以用str_replace()是不行的

preg_replace()是可以实现的,可惜用了正则,

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6933')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6933>

$str=preg_replace('/abc/','xyz',$str,1);
echo $str;

有没有不用正则的,嗯可以这样

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5699')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5699>

$replace='xyz';
if(($position=strpos($str,$replace))!==false){
     $leng=strlen($replace);
    $str=substr_replace($str,'xyz',$position,$leng);
}
echo $str;

您可能感兴趣的文章:
php5 字符串处理函数汇总
php表单提交特殊字符过滤方法
PHP 替换字符串中的一些字符方法介绍
PHP替换字符串(只替换首个字符串)
php字符串函数有哪些
php查找字符串中http地址
php字符串-php入门教程(3)
有关匹配中文的正则(GB2312/utf-8)介绍
php 字符串替换函数学习
php 字符串的声明方法

[关闭]
~ ~