• str_replace 用法及范例 - [PHP newbie]

    2007-12-04

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://magius.blogbus.com/logs/11486136.html

        格式:
    [@str_replace("要替换的旧内容", "要取代原内容的新字符", $被替换内容的变量名)]
    [@str_replace(array('旧1','旧2','旧3'), array('新1','新2','新3'), $被替换内容的变量名)]
    [@str_replace(array('旧1','旧2','旧3'), '新内容', $被替换内容的变量名)]
    实例:
    多对一替换:想把内容字段里所有的

     

    标签清除掉,替换成空

    [@str_replace(array('

    ','

    '), '', $Content)]
    一对一替换:想把内容字段里所有的
    标签换成

     

    [@str_replace('
    ', '

    ', $Content)]
    多对多替换:想把内容字段里的
    换成
    , 同时


    ,把全清除

    [@str_replace(array('
    ', '

    ','

    '), array('
    ','
    ',''), $Content)]

    // Provides:
    $bodytag = str_replace("%body%", "black", "");
    echo $bodytag;
    // Provides: Hll Wrld f PHP
    $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
    $onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
    echo $onlyconsonants;
    // Provides: You should eat pizza, beer, and ice cream every day
    $phrase    = "You should eat fruits, vegetables, and fiber every day.";
    $healthy = array("fruits", "vegetables", "fiber");
    $yummy     = array("pizza", "beer", "ice cream");
    $newphrase = str_replace($healthy, $yummy, $phrase);
    echo $newphrase;
    // Use of the count parameter is available as of PHP 5.0.0
    $str = str_replace("ll", "", "good golly miss molly!", $count);
    echo $count; // 2
    $str="

    this is test


    this.

    hello

    ";
    print str_replace(array('

    ','

    ','
    '),array('
    ','
    ',''),$str)
    ?>

    php 的sub_replace函数对应的js函数为
    String.prototype.replaceAll = function(search, replace){
    var regex = new RegExp(search, "g");
    return this.replace(regex, replace);

    var str = '

    asfdafd

    asfdafd

    asfdafd

    ';
    alert(str.replaceAll('', '
    '));
    该文章转载自脚本之家:http://www.jb51.net/html/200704/66/9727.htm
     

    历史上的今天:

    tags 表思路 2007-12-04
    tags 2007-12-04

    收藏到:Del.icio.us




    评论

  • testest
    magius回复test说:
    针对回复
    2007-12-04 15:13:46