dj 实用网站 mp3 app
  • 二级建造师各省报名时间
  • 低学历学历提升指南
  • 注册消防工程师报考指南
  • 学历提升的多种途径和方法
  • 学历提升的方法和技巧
  • 二级建造师,报名条件查询
  • 初中学历能报名吗
  • 无学历想考专科
  • 学历提升哪家好
  • 上班族学历提升首选
  • 初中文化也可以自考本科吗
  • 无学历升专科
  • 挖掘机找活二手买卖APP
  • 免费在线实用工具查询网站
  • 学历教育提升方式
  • 初中学历毕业后升本科
  • English | 简体中文 | 繁體中文
    查询

    substr_replace()函数—用法及示例

    「 将字符串的一部分替换为另一个字符串 」


    函数名:substr_replace()

    函数描述:substr_replace() 函数用于将字符串的一部分替换为另一个字符串。

    参数:

    • string:必需。原始字符串。
    • replacement:必需。替换字符串。
    • start:必需。替换的起始位置。如果 start 是正数,则从字符串的 start 位置开始替换。如果是负数,则从字符串末尾的 start 位置开始替换。
    • length:可选。替换的长度。如果省略,则替换从 start 位置开始到字符串末尾的所有字符。
    • encoding:可选。指定字符编码。常用的编码有 "UTF-8"、"ISO-8859-1" 等。

    返回值:返回替换后的字符串。

    示例:

    $str = "Hello, world!";
    $replacement = "PHP";
    $start = 7;
    
    // 替换字符串的一部分为 "PHP"
    $result = substr_replace($str, $replacement, $start);
    echo $result; // 输出 "Hello, PHP!"
    
    $str2 = "Hello, world!";
    $replacement2 = "PHP";
    $start2 = -6;
    $length2 = 5;
    
    // 替换字符串的一部分为 "PHP",从倒数第6个字符开始,替换长度为5
    $result2 = substr_replace($str2, $replacement2, $start2, $length2);
    echo $result2; // 输出 "Hello, PHP!"
    
    $str3 = "Hello, world!";
    $replacement3 = "PHP";
    $start3 = 7;
    $length3 = 5;
    
    // 替换字符串的一部分为 "PHP",从第7个字符开始,替换长度为5
    $result3 = substr_replace($str3, $replacement3, $start3, $length3);
    echo $result3; // 输出 "Hello, PHP, world!"
    

    注意:substr_replace() 函数在 PHP 5 及以上版本可用。

    补充纠错
    上一个函数: SVM::getOptions()函数
    下一个函数: substr_count()函数
    热门PHP函数
    分享链接