正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。
正则表达式是由普通字符(例如字符 a 到 z)以及特殊字符(称为"元字符")组成的文字模式。模式描述在搜索文本时要匹配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。
实例
<?php
$str = <<< EOT
<a href="www/app/a/2QRN7v" rel="external nofollow" >
<p class="phonebg">
<img src="http://www/template9/yunqingjian/jianjie/68.jpg" >
<p class="phoneclick"></p>
<p>幸福领地</p>
</p>
</a>
<a href="www/app/a/uqARNv" rel="external nofollow" >
<p class="phonebg">
<img src="http://www/template9/yunqingjian/jianjie/69.jpg" >
<p class="phoneclick"></p>
<p>一世情长</p>
</p>
</a>
EOT;
if(preg_match_all('%<p.*?>(.*?)</p>%si', $str, $matches)) {
$arr[0][] = $matches[1];
}
if(preg_match_all('/src="([^<]*)" >/i', $str, $matches)) {
$arr[1][] = $matches[1];
}
print_r($arr);
exit;
?>运行结果如下:
Array
(
[0] => Array
(
[0] => Array
(
[0] => 幸福领地
[1] => 一世情长
)
)
[1] => Array
(
[0] => Array
(
[0] => http://www/template9/yunqingjian/jianjie/68.jpg
[1] => http://www/template9/yunqingjian/jianjie/69.jpg
)
)
)以上就是php 正则表达式示例的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号