当前位置 博文首页 > 文章内容

    php中查找替换函数有哪些

    作者:shunshunshun18 栏目:未分类 时间:2021-12-28 11:19:44

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    替换函数:1、str_ireplace();2、str_replace();3、substr_replace();4、array_replace();5、array_replace_recursive();6、array_splice()。

    本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑

    php字符串查找替换函数

    • str_ireplace():替换字符串中的一些字符(对大小写不敏感)。

    • str_replace():替换字符串中的一些字符(对大小写敏感)。

    • substr_replace():把字符串的一部分替换为另一个字符串。

    str_ireplace() 和 str_replace() 函数

    str_ireplace() 和 str_replace 使用新的字符串替换原来字符串中指定的特定字符串,str_replace 区分大小写,str_ireplace() 不区分大小写,两者语法相似。

    str_ireplace() 的语法如下:

    mixed str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

    该函数返回一个字符串或者数组。该字符串或数组是将 subject 中全部的 search 用 replace 替换(忽略大小写)之后的结果。参数 count 表示执行替换的次数。

    使用示例如下:

    <?php
    $str = 'hello,world,hello,world';
    $replace = 'hi';
    $search = 'hello';
    echo str_ireplace($search, $replace, $str);
    ?>

    执行以上代码的输出结果为:

    hi,world,hi,world

    substr_replace() 函数

    substr_replace() 函数的语法如下:

    mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )

    substr_replace() 在字符串 string 的副本中将由 start 和可选的 length 参数限定的子字符串使用 replacement 进行替换。

    如果 start 为正数,替换将从 string 的 start 位置开始。如果 start 为负数,替换将从 string 的倒数第 start 个位置开始。

    如果设定了 length 参数并且为正数,就表示 string 中被替换的子字符串的长度。如果设定为负数,就表示待替换的子字符串结尾处距离 string 末端的字符个数。如果没有提供此参数,那么默认为 strlen(string)(字符串的长度)。当然,如果 length 为 0,那么这个函数的功能为将 replacement 插入 string 的 start 位置处。

    该函数的使用示例如下:

    <?php
    $str = 'hello,world,hello,world';
    $replace = 'hi';
    echo substr_replace($str, $replace, 0,5);
    ?>

    以上代码的执行结果为:

    hi,world,hello,world

    php数组查找替换函数

    • array_replace():使用后面数组的值替换第一个数组的值。

    • array_replace_recursive():递归地使用后面数组的值替换第一个数组的值。

    • array_splice():删除并替换数组中指定的元素。

    array_splice() 函数

    array_splice() 函数用来删除数组的一部分元素;你可以直接删除,也可以用其它值来替代。

    array_splice() 语法如下:

    array array_splice ( array &$arr, int $start [, int $length = 0 [, mixed $replacement ]] )

    参数说明:

    • arr 表示一个数组。
    • start 表示开始删除的位置(下标):
      • 如果 start 为正数,则从前往后删除。
      • 如果 start 为负数,则从距离 arr 末端 -start 的位置开始,从后往前删除。例如 -2 意味着从数组的倒数第二个元素开始。
    • length 是可选参数,表示删除的元素个数:
      • 如果 length 为正数,那么就表示删除 length 个元素;
      • 如果 length 为负数,那么将删除从 start 开始,到数组末尾倒数 length 为止的所有元素;
      • 如果省略,那么将删除从 start 开始,一直到数组末尾的所有元素。
    • replacement 是可选参数,表示要替换的值。如果 replacement 有多个值就需要设置为数组,如果只有一个值可以不设置为数组。

    如果 start 和 length 组合的结果是不会删除任何元素,那么 replacement 所包含的值将被插入 start 指定的位置。

    注意,使用 replacement 替换数组元素不会保留原来的键名。

    返回值:返回由被删除的元素组成的数组。

    函数的使用示例如下:

    <?php
    $arr = array("red", "green", "blue", "yellow");
    array_splice($arr, 2);
    print_r($arr);
    //$arr 现在变成 array("red", "green")
    $arr = array("red", "green", "blue", "yellow");
    array_splice($arr, 1, -1);
    print_r($arr);
    //$arr 现在变成 array("red", "yellow")
    $arr = array("red", "green", "blue", "yellow");
    array_splice($arr, 1, count($arr), "orange");
    print_r($arr);
    //$arr 现在变成 array("red", "orange")
    $arr = array("red", "green", "blue", "yellow");
    array_splice($arr, -1, 1, array("black", "maroon"));
    print_r($arr);
    //$input 现在变成 array("red", "green", "blue", "black", "maroon")
    $arr = array("red", "green", "blue", "yellow");
    array_splice($arr, 3, 0, "purple");
    print_r($arr);
    //$arr 现在变成 array("red", "green", "blue", "purple", "yellow");
    ?>

    执行以上程序的输出结果如下:

    Array
    (
        [0] => red
        [1] => green
    )
    Array
    (
        [0] => red
        [1] => yellow
    )
    Array
    (
        [0] => red
        [1] => orange
    )
    Array
    (
        [0] => red
        [1] => green
        [2] => blue
        [3] => black
        [4] => maroon
    )
    Array
    (
        [0] => red
        [1] => green
        [2] => blue
        [3] => purple
        [4] => yellow
    )

    推荐学习:《》