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;">"Title: Hello world!<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #0000ff;">$Text</span> .= <span style="color: #ff0000;">"Author: Jonas<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #0000ff;">$Text</span> .= <span style="color: #ff0000;">"This is a example message!<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #0000ff;">$Text</span> .= <span style="color: #ff0000;">"Title: Entry 2<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #0000ff;">$Text</span> .= <span style="color: #ff0000;">"Author: Sonja<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #0000ff;">$Text</span> .= <span style="color: #ff0000;">"Hello world, what's up!<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<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;">(</span><span style="color: #0000ff;">$Match</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// Entire matched section: </span>
<span style="color: #808080; font-style: italic;">// --> /.../</span>
<span style="color: #0000ff;">$EntireSection</span> = <span style="color: #0000ff;">$Match</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">]</span>;
<span style="color: #808080; font-style: italic;">// --> "\nTitle: Hello world!"</span>
<span style="color: #808080; font-style: italic;">// Key </span>
<span style="color: #808080; font-style: italic;">// --> ([a-z0-9]+)</span>
<span style="color: #0000ff;">$Key</span> = <span style="color: #0000ff;">$Match</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">]</span>;
<span style="color: #808080; font-style: italic;">// --> "Title"</span>
<span style="color: #808080; font-style: italic;">// Value </span>
<span style="color: #808080; font-style: italic;">// --> ([^\n\r]+)</span>
<span style="color: #0000ff;">$Value</span> = <span style="color: #0000ff;">$Match</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">]</span>;
<span style="color: #808080; font-style: italic;">// --> "Hello world!"</span>
<span style="color: #808080; font-style: italic;">// Add some bold (<b>) tags to around the key to</span>
<span style="color: #b1b100;">return</span> <span style="color: #ff0000;">'<b>'</span> . <span style="color: #0000ff;">$Key</span> . <span style="color: #ff0000;">'</b>: '</span> . <span style="color: #0000ff;">$Value</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// The regular expression will extract and pass all "key: value" pairs to</span>
<span style="color: #808080; font-style: italic;">// the "RewriteText" 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;">(</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;">"RewriteText"</span>, <span style="color: #0000ff;">$Text</span><span style="color: #66cc66;">)</span>;
<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 正则替换回调方法用法,