Home > Backend Development > PHP Tutorial > PHP 正则(二)

PHP 正则(二)

WBOY
Release: 2016-06-13 12:16:10
Original
1035 people have browsed it

PHP 正则(2)

PHP 正则

有两个比较重要的方法 preg_match()preg_replace(); 给一个简单的例子。要对这个进行匹配,用到的模式如下

<code class="language-php hljs "><span class="hljs-variable">$str</span> = <span class="hljs-string">"123123,123123,123123,213123,234234"</span>;<span class="hljs-variable">$model</span> = <span class="hljs-string">"/^([0-9]*),(([0-9]*),))*([0-9]*)$/"</span>;<span class="hljs-variable">$match</span> = <span class="hljs-keyword">array</span>();<span class="hljs-variable">$ret</span> = preg_match(<span class="hljs-variable">$model</span>, <span class="hljs-variable">$str</span>, <span class="hljs-variable">$match</span>);<span class="hljs-variable">$match</span> 中就有匹配到的内容。</code>
Copy after login

贪婪匹配非贪婪匹配 可以参考下面的例子

<code class="language-php hljs "><span class="hljs-variable">$eee</span> = <span class="hljs-string">"(12)-123 23-(233)"</span>;<span class="hljs-comment">//贪婪匹配的例子 这个模式只能匹配 一个。</span><span class="hljs-variable">$preg</span> = <span class="hljs-string">"/\(.*\)/"</span>;<span class="hljs-variable">$match</span> = <span class="hljs-keyword">array</span>();<span class="hljs-variable">$ans</span> = preg_match_all(<span class="hljs-variable">$preg</span>,<span class="hljs-variable">$eee</span>,<span class="hljs-variable">$match</span>);var_dump(<span class="hljs-variable">$match</span>);<span class="hljs-comment">// 结果是只能匹配一个模式。因为第一个从整体看来,是一个完整的括号,所以会匹配整个结果。</span><span class="hljs-comment">//非贪婪匹配模式,可以把所有的括号中的内容都找出来,却别就是*后面的问号。</span><span class="hljs-variable">$preg</span> = <span class="hljs-string">"/\(.*?\)/"</span>;<span class="hljs-variable">$match</span> = <span class="hljs-keyword">array</span>();<span class="hljs-variable">$ans</span> = preg_match_all(<span class="hljs-variable">$preg</span>,<span class="hljs-variable">$eee</span>,<span class="hljs-variable">$match</span>);var_dump(<span class="hljs-variable">$match</span>);</code>
Copy after login

正则替换

preg_replace() 可以对 $str 中的内容进行替换。还是上面的例子,这次我们把$eee中国年的括号里面的数字替换成字符串haha.可以设计出如下的方法。注意 preg_replace 的用法。$pattern 和 $replace 都是array,下面是具体的匹配模式。

<code class="language-php hljs ">按照正则表达式的规则对匹配的内容进行替换。<span class="hljs-variable">$eee</span> = <span class="hljs-string">"(12)-123 23-(233)"</span>;<span class="hljs-variable">$preg</span> = <span class="hljs-keyword">array</span>(<span class="hljs-string">"/\(.*?\)/"</span>);<span class="hljs-variable">$replace</span> = <span class="hljs-keyword">array</span>(<span class="hljs-string">"haha"</span>);<span class="hljs-variable">$ans</span> = preg_replace(<span class="hljs-variable">$preg</span>,<span class="hljs-variable">$replace</span>,<span class="hljs-variable">$eee</span>);var_dump(<span class="hljs-variable">$ans</span>);</code>
Copy after login

上面是比较基本的用法,下面这个稍微复杂一些。

<code class="language-php hljs "><span class="hljs-variable">$str</span> = preg_replace(<span class="hljs-string">"/(<a.*?>)(.*?)(<\/a>)/"</span>,<span class="hljs-string">'\1<span class="link">\2</span>\3'</span>, <span class="hljs-variable">$str</span>); <span class="hljs-comment">// 其中用了三个子模式(每个圆括号中内容为一个子模式),第一个是链接开始标签,第二个是链接文本</span><span class="hljs-comment">// 第三个是</a> 这个正则正则替换的功能是,给所有的链接加上<span>标签,给链接的内容加上样式。</span><span class="hljs-comment">// 通过学习上面的表达是,我们可以将我们前面的例子 转化成这样。</span><span class="hljs-variable">$eee</span> = <span class="hljs-string">"whie2ch(12)-123 23-function(233)"</span>;<span class="hljs-variable">$preg</span> =<span class="hljs-string">"/(\w*)\(.*?\)/"</span>;<span class="hljs-variable">$replace</span> =<span class="hljs-string">"\1word\2bb"</span>; <span class="hljs-comment">// 将变量换成word,将括号及括号里面的内容换成bb \1 \2 代表第几个括号里面匹配的内容。</span><span class="hljs-variable">$ans</span> = preg_replace(<span class="hljs-variable">$preg</span>,<span class="hljs-variable">$replace</span>,<span class="hljs-variable">$eee</span>);var_dump(<span class="hljs-variable">$ans</span>);</code>
Copy after login

PHP 修正符

<code class="language-php hljs "><span class="hljs-variable">$eee</span> =<span class="hljs-string">"whie2ch(1 \n2)-123 n 23-function nhehe(23 \n3) sfsdf(24 \n5)"</span>;<span class="hljs-variable">$preg</span> =<span class="hljs-string">"/\(.*?\)/"</span>;<span class="hljs-comment">//$replace ="\1word\2bb";</span><span class="hljs-variable">$match</span> = <span class="hljs-keyword">array</span>();<span class="hljs-variable">$ans</span> =preg_match_all(<span class="hljs-variable">$preg</span>,<span class="hljs-variable">$eee</span>,<span class="hljs-variable">$match</span>);var_dump(<span class="hljs-variable">$match</span>);<span class="hljs-comment">// 这样,发现是匹配不到括号里面的内容的。因为.不包括换行。</span><span class="hljs-comment">// 使用方法,把上面的 $preg 改成下面这样。</span><span class="hljs-variable">$preg</span> =<span class="hljs-string">"/\(.*?\)/s"</span>;重新运行程序,发现所有的内容都匹配出来了。</code>
Copy after login

PHP 的几个修正符如下

修正符 含义
s 让.包括换行,可以实现跨行匹配
U 让?默认匹配成贪婪状态
i 忽略大小写
u 模式串被当做UTF8
m 当 设定了此修正符,行起始(^)和行结束($)除了匹配整个字符串开头和结束外,还分别匹配其中的换行符(\n)的之后和之前

PHP修正符 的使用方式入上面的程序所示。将/your_regrex/X,其中X代表你的修正符,根据需要选择。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template