zoukankan      html  css  js  c++  java
  • str_replace函数的使用规则和案例详解

    str_replace函数的使用规则和案例详解

    str_replace函数的简单调用:

    <?php
    $str = '苹果很好吃。';
    //请将变量$str中的苹果替换成香蕉
    $strg = str_replace('苹果','香蕉',$str);
    echo $strg;
    ?>

    输出结果为:“香蕉很好吃”

    解释:在str中将"苹果"替换成"香蕉", 所以原本的$str"苹果很好吃" 被改成了 "香蕉很好吃"

    str_replace函数的扩展:

    <?php

    $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
    $onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

    echo $onlyconsonants;
    ?>

    输出结果为“Hll Wrld f PHP”

    解析:$vowels为需要替换的数据,''''表示把要替换的换成空白, "Hello World of PHP"则是目标原始数据。 所以就是只要是$vowels里面的字母都被替换成空白, 所以"Hello World of PHP"就成了"Hllo Wrld f PHP"

    str_replace(a,b,c)

    函数str_replace中,a为要换的数据,它可以是文字,字母或者中文等字符串或者数组, b为替换成的值,可以为字符串或者数级, c则是需要改的目标

    如果再不清楚,看上面的两个例子。

  • 相关阅读:
    获取web应用路径 // "/" 表示class 根目录
    C++ Knowledge series Inheritance & RTTI & Exception Handling
    ATL
    GCC & Maker
    NoSQL(Not Only SQL)
    ECMAScript Regex
    C++ Knowledge series Conversion & Constructor & Destructor
    Cloud Computing
    C++ Knowledge series STL & Const
    Java Knowledge series 7
  • 原文地址:https://www.cnblogs.com/llzhang123/p/9234848.html
Copyright © 2011-2022 走看看