久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php怎么實現(xiàn)正則替換內(nèi)容

      php正則替換內(nèi)容的方法:1、用preg_replace(),可執(zhí)行正則表達式的搜索和替換,語法“preg_filter (正則式,替換值,數(shù)據(jù))”;2、用preg_filter(),語法“preg_filter(正則式,替換值,數(shù)據(jù))”。

      php怎么實現(xiàn)正則替換內(nèi)容

      本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦

      在php中,可以使用下面兩個函數(shù)來實現(xiàn)正則替換內(nèi)容

      • preg_replace():執(zhí)行一個正則表達式的搜索和替換

      • preg_filter():執(zhí)行一個正則表達式的搜索和替換

      1、使用preg_replace()函數(shù)

      preg_replace() 函數(shù)可以執(zhí)行正則表達式的搜索和替換,是一個強大的字符串替換處理函數(shù)。語法格式:

      preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

      搜索 subject 中匹配 pattern 的部分, 以 replacement 進行替換。

      參數(shù)說明:

      • $pattern: 要搜索的模式,可以是字符串或一個字符串數(shù)組。

      • $replacement: 用于替換的字符串或字符串數(shù)組。

      • $subject: 要搜索替換的目標字符串或字符串數(shù)組。

      • $limit: 可選,對于每個模式用于每個 subject 字符串的最大可替換次數(shù)。 默認是-1(無限制)。

      • $count: 可選,為替換執(zhí)行的次數(shù)。

      返回值

      • 如果 subject 是一個數(shù)組, preg_replace() 返回一個數(shù)組, 其他情況下返回一個字符串。

      • 如果匹配被查找到,替換后的 subject 被返回,其他情況下 返回沒有改變的 subject。如果發(fā)生錯誤,返回 NULL。

      示例1:將 hello 替換為 phpcn

      <?php $string = 'hello 123, 456'; $pattern = '/(w+) (d+), (d+)/i'; $replacement = 'phpcn ${2},$3'; echo preg_replace($pattern, $replacement, $string); ?>

      php怎么實現(xiàn)正則替換內(nèi)容

      示例2:刪除空格字符

      <?php header('content-type:text/html;charset=utf-8');    $str = '歡  迎  來 到 p h p 中 文 網(wǎng)'; $str = preg_replace('/s+/', '', $str); // 將會改變?yōu)?#39;歡迎來到php中文網(wǎng)' echo $str; ?>

      php怎么實現(xiàn)正則替換內(nèi)容

      2、使用preg_filter()函數(shù)

      preg_filter() 函數(shù)也用于執(zhí)行一個正則表達式的搜索和替換,等價于preg_replace() 函數(shù);不同的是 preg_filter() 函數(shù)只返回匹配成功的結(jié)果,而 preg_replace() 返回所有結(jié)果,不管是否匹配成功。

      示例:比較preg_filter() 和preg_replace()

      <?php header('content-type:text/html;charset=utf-8');    $subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4');  $pattern = array('/d/', '/[a-z]/', '/[1a]/');  $replace = array('A:$0', 'B:$0', 'C:$0');    echo "preg_filter 返回值:"; var_dump(preg_filter($pattern, $replace, $subject));    echo "preg_replace 返回值:"; var_dump(preg_replace($pattern, $replace, $subject));  ?>

      php怎么實現(xiàn)正則替換內(nèi)容

      推薦學習:《PHP視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號