PHPRegex的逆向引用與子模式 php preg_replace應用

來源:互聯網
上載者:User

標籤:style   ar   os   sp   div   art   html   line   as   

mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit])

功能

在 subject 中搜尋 pattern 模式的匹配項並替換為 replacement。如果指定了 limit,則僅替換 limit 個匹配,如果省略 limit 或者其值為 -1,則所有的匹配項都會被替換。
replacement可以包含\\n形式或$n形式的逆向引用,n可以為0到99,\\n表示匹配pattern第n個子模式的文本,\\0表示匹配整個pattern的文本。

子模式

$pattern參數中被圓括弧括起來的Regex,子模式的數目即從左至右圓括弧的數目。(pattern即模式)

例子

需求:把OT函數由OT(a)改成OT(a,b),加一個參數

<?php

$str = <<<EOD
_Article_Source=OT(‘.div(PosID(‘bd_nav‘)>0)[1#].a(PosHTML(‘/b*‘)>0)[1#]‘)
;_Article_Author=OT(‘.div(PosClass(‘doc_top p9‘)>0)[1#].a(PosClass(‘b‘)>0)[1#]‘)

EOD;

$reg=‘/(=OT\(.+)(\)[\r,\n])/‘;

$replacement=‘$1,n$2‘;
echo preg_replace($reg,$replacement,$str);
?>

PHPRegex的逆向引用與子模式 php preg_replace應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.