9.3.6 字符串的替换(preg_replace)1

9.3.6 字符串的替换(preg_replace)1

1.php

<?php
    header("Content-Type:text/html;charset=utf-8");
/*  $reg = '/(https?|ftps?):\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn)([\w-\.\/\=\?\&\%]*)?/';
    $reg = '/\w+([+-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* /i';

 *  分割、匹配、查找、替换
 *
 *  1. 字符串处理函数 (处理快, 但有一些做不到)
 *
 *  2. 正则表达式函数  (功能强大,但效率要低)
 *
 *
 *  注意:如果可以直接使用字符串处理函数处理的字符串,就不要使用正则处理
 *
 *
 *  
 * 匹配查找:
 *
 *  strstr  strpos substr
 *
 *
 * 正则匹配查找
 *
 *  preg_match()   preg_match_all()   preg_grep();
 *
 *
 * 分割:
 *  explode()   implode() -- join()
 *
 *
 *正则表达式
    preg_split()  
 *
 *
 *
 * 替换
 *  字符串处理  str_replace()
 *
 *
 *  正则表达式  preg_replace()   
 */




    $str ="如果没有一些特殊的<b>替换</b>需求(<u>比如正则表达式</u>),你应该使用该函数替换 <font color='red'>ereg_replace()</font> 和 preg_replace()。 ";

    $html = "/\<[\/\!]*?[^\<\>]*?\>/is";


    $newstr = preg_replace($html, "", $str);

    echo $str."<br>";
    echo $newstr."<br>";

2.php

<?php
    header("Content-Type:text/html;charset=utf-8");
/*  $reg = '/(https?|ftps?):\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn)([\w-\.\/\=\?\&\%]*)?/';
    $reg = '/\w+([+-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* /i';

 *  分割、匹配、查找、替换
 *
 *  1. 字符串处理函数 (处理快, 但有一些做不到)
 *
 *  2. 正则表达式函数  (功能强大,但效率要低)
 *
 *
 *  注意:如果可以直接使用字符串处理函数处理的字符串,就不要使用正则处理
 *
 *
 *  
 * 匹配查找:
 *
 *  strstr  strpos substr
 *
 *
 * 正则匹配查找
 *
 *  preg_match()   preg_match_all()   preg_grep();
 *
 *
 * 分割:
 *  explode()   implode() -- join()
 *
 *
 *正则表达式
    preg_split()  
 *
 *
 *
 * 替换
 *  字符串处理  str_replace()
 *
 *
 *  正则表达式  preg_replace()   
 */




    $str ="如果没有一些特殊的<b>替换</b>需求(<u>比如正则表达式</u>),你应该使用该函数替换 <font color='red'>ereg_replace()</font> 和 preg_replace()。 ";

    $html = "/\<[\/\!]*?[^\<\>]*?\>/is";


    $newstr = preg_replace($html, "", $str, 4);

    echo $str."<br>";
    echo $newstr."<br>";

test.php

<?php
    header("Content-Type:text/html;charset=utf-8");
/*  $reg = '/(https?|ftps?):\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn)([\w-\.\/\=\?\&\%]*)?/';
    $reg = '/\w+([+-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* /i';

 *  分割、匹配、查找、替换
 *
 *  1. 字符串处理函数 (处理快, 但有一些做不到)
 *
 *  2. 正则表达式函数  (功能强大,但效率要低)
 *
 *
 *  注意:如果可以直接使用字符串处理函数处理的字符串,就不要使用正则处理
 *
 *
 *  
 * 匹配查找:
 *
 *  strstr  strpos substr
 *
 *
 * 正则匹配查找
 *
 *  preg_match()   preg_match_all()   preg_grep();
 *
 *
 * 分割:
 *  explode()   implode() -- join()
 *
 *
 *正则表达式
    preg_split()  
 *
 *
 *
 * 替换
 *  字符串处理  str_replace()
 *
 *
 *  正则表达式  preg_replace()   
 *
 *
 *  strtoupper();
 */





$str =  array(
    "如果没有一些http://www.baidu.com特殊的<b>替换</b>需5求(<u>比如正则表达式</u>),你应http://www.lampbrother.net该使用该http://bbs.brophp.org函数替9换 <font color='red'>ereg_replace()</font> 和9 preg_replace()。 ",
    "如果没有一些http://www.baidu.com特殊的<b>替换</b>需5求(<u>比如正则表达式</u>),你应http://www.lampbrother.net该使用该http://bbs.brophp.org函数替9换 <font color='red'>ereg_replace()</font> 和9 preg_replace()。 ",
    "如果没有一些http://www.baidu.com特殊的<b>替换</b>需5求(<u>比如正则表达式</u>),你应http://www.lampbrother.net该使用该http://bbs.brophp.org函数替9换 <font color='red'>ereg_replace()</font> 和9 preg_replace()。 ",
    "如果没有一些http://www.baidu.com特殊的<b>替换</b>需5求(<u>比如正则表达式</u>),你应http://www.lampbrother.net该使用该http://bbs.brophp.org函数替9换 <font color='red'>ereg_replace()</font> 和9 preg_replace()。 "
    
    
    
    );

    $reg  = array(
            '/\<[\/\!]*?[^\<\>]+?\>/is',
            '/(https?|ftps?):\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn)([\w-\.\/\=\?\&\%]*)?/e', 
            '/\d/'
        );

    $rep = array(
            '',
            '"<a href=\'$1://$2.$3.$4\'>".strtoupper("$1://$2.$3.$4")."</a>"',
            '@'
        );


    $newstr = preg_replace($reg, $rep, $str);

    echo '<pre>';
    print_r($str)."<br>";
    print_r($newstr)."<br>";
    echo '</pre>';
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容