9.3.7 字符串的替換(preg_replace)2

9.3.7 字符串的替換(preg_replace)2

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. 字符串處理函數(shù) (處理快, 但有一些做不到)
 *
 *  2. 正則表達(dá)式函數(shù)  (功能強(qiáng)大,但效率要低)
 *
 *
 *  注意:如果可以直接使用字符串處理函數(shù)處理的字符串,就不要使用正則處理
 *
 *
 *  
 * 匹配查找:
 *
 *  strstr  strpos substr
 *
 *
 * 正則匹配查找
 *
 *  preg_match()   preg_match_all()   preg_grep();
 *
 *
 * 分割:
 *  explode()   implode() -- join()
 *
 *
 *正則表達(dá)式
    preg_split()  
 *
 *
 *
 * 替換
 *  字符串處理  str_replace()
 *
 *
 *  正則表達(dá)式  preg_replace()   
 */




    $str ="如果沒有一些特殊的<b>替換</b>需求(<u>比如正則表達(dá)式</u>),你應(yīng)該使用該函數(shù)替換 <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. 字符串處理函數(shù) (處理快, 但有一些做不到)
 *
 *  2. 正則表達(dá)式函數(shù)  (功能強(qiáng)大,但效率要低)
 *
 *
 *  注意:如果可以直接使用字符串處理函數(shù)處理的字符串,就不要使用正則處理
 *
 *
 *  
 * 匹配查找:
 *
 *  strstr  strpos substr
 *
 *
 * 正則匹配查找
 *
 *  preg_match()   preg_match_all()   preg_grep();
 *
 *
 * 分割:
 *  explode()   implode() -- join()
 *
 *
 *正則表達(dá)式
    preg_split()  
 *
 *
 *
 * 替換
 *  字符串處理  str_replace()
 *
 *
 *  正則表達(dá)式  preg_replace()   
 */




    $str ="如果沒有一些特殊的<b>替換</b>需求(<u>比如正則表達(dá)式</u>),你應(yīng)該使用該函數(shù)替換 <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. 字符串處理函數(shù) (處理快, 但有一些做不到)
 *
 *  2. 正則表達(dá)式函數(shù)  (功能強(qiáng)大,但效率要低)
 *
 *
 *  注意:如果可以直接使用字符串處理函數(shù)處理的字符串,就不要使用正則處理
 *
 *
 *  
 * 匹配查找:
 *
 *  strstr  strpos substr
 *
 *
 * 正則匹配查找
 *
 *  preg_match()   preg_match_all()   preg_grep();
 *
 *
 * 分割:
 *  explode()   implode() -- join()
 *
 *
 *正則表達(dá)式
    preg_split()  
 *
 *
 *
 * 替換
 *  字符串處理  str_replace()
 *
 *
 *  正則表達(dá)式  preg_replace()   
 *
 *
 *  strtoupper();
 */





$str =  array(
    "如果沒有一些http://www.baidu.com特殊的<b>替換</b>需5求(<u>比如正則表達(dá)式</u>),你應(yīng)http://www.lampbrother.net該使用該http://bbs.brophp.org函數(shù)替9換 <font color='red'>ereg_replace()</font> 和9 preg_replace()。 ",
    "如果沒有一些http://www.baidu.com特殊的<b>替換</b>需5求(<u>比如正則表達(dá)式</u>),你應(yīng)http://www.lampbrother.net該使用該http://bbs.brophp.org函數(shù)替9換 <font color='red'>ereg_replace()</font> 和9 preg_replace()。 ",
    "如果沒有一些http://www.baidu.com特殊的<b>替換</b>需5求(<u>比如正則表達(dá)式</u>),你應(yīng)http://www.lampbrother.net該使用該http://bbs.brophp.org函數(shù)替9換 <font color='red'>ereg_replace()</font> 和9 preg_replace()。 ",
    "如果沒有一些http://www.baidu.com特殊的<b>替換</b>需5求(<u>比如正則表達(dá)式</u>),你應(yīng)http://www.lampbrother.net該使用該http://bbs.brophp.org函數(shù)替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>';

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容