php Preg替换回调示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Preg替换回调示例相关的知识,希望对你有一定的参考价值。

<span style="color: #808080; font-style: italic;">// Define a dummy text, for testing...</span>
<span style="color: #0000ff;">$Text</span>  = <span style="color: #ff0000;">&quot;Title: Hello world!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #0000ff;">$Text</span> .= <span style="color: #ff0000;">&quot;Author: Jonas<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #0000ff;">$Text</span> .= <span style="color: #ff0000;">&quot;This is a example message!<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #0000ff;">$Text</span> .= <span style="color: #ff0000;">&quot;Title: Entry 2<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #0000ff;">$Text</span> .= <span style="color: #ff0000;">&quot;Author: Sonja<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #0000ff;">$Text</span> .= <span style="color: #ff0000;">&quot;Hello world, what's up!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// This function will replace specific matches</span>
<span style="color: #808080; font-style: italic;">// into a new form</span>
<span style="color: #000000; font-weight: bold;">function</span> RewriteText<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Match</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// Entire matched section: </span>
    <span style="color: #808080; font-style: italic;">// --&gt; /.../</span>
    <span style="color: #0000ff;">$EntireSection</span>  = <span style="color: #0000ff;">$Match</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #808080; font-style: italic;">// --&gt; &quot;\nTitle: Hello world!&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// Key </span>
    <span style="color: #808080; font-style: italic;">// --&gt; ([a-z0-9]+)</span>
    <span style="color: #0000ff;">$Key</span>            = <span style="color: #0000ff;">$Match</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #808080; font-style: italic;">// --&gt; &quot;Title&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// Value </span>
    <span style="color: #808080; font-style: italic;">// --&gt; ([^\n\r]+)</span>
    <span style="color: #0000ff;">$Value</span>        = <span style="color: #0000ff;">$Match</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #808080; font-style: italic;">// --&gt; &quot;Hello world!&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// Add some bold (&lt;b&gt;) tags to around the key to</span>
    <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">'&lt;b&gt;'</span> . <span style="color: #0000ff;">$Key</span> . <span style="color: #ff0000;">'&lt;/b&gt;: '</span> . <span style="color: #0000ff;">$Value</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// The regular expression will extract and pass all &quot;key: value&quot; pairs to</span>
<span style="color: #808080; font-style: italic;">// the &quot;RewriteText&quot; function that is definied above</span>
<span style="color: #0000ff;">$NewText</span> = <a href="http://www.php.net/preg_replace_callback"><span style="color: #000066;">preg_replace_callback</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/[<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>]([a-z0-9]+): ([^<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span>]+)/i'</span>, <span style="color: #ff0000;">&quot;RewriteText&quot;</span>, <span style="color: #0000ff;">$Text</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Print the new modified text</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$NewText</span>;

以上是关于php Preg替换回调示例的主要内容,如果未能解决你的问题,请参考以下文章

preg_replace_callback 正则替换回调方法用法,

preg_replace_callback使用方法

PHP:Preg 替换括号?

php正则表达式的匹配与替换

php preg_replace遇到替换本身有括号的内容怎么办

PHP preg_replace() 正则替换所有符合条件的字符串