Home > Article > Backend Development > How to implement regular matching operation in PHP
This article mainly introduces the PHP regular matching operation, and combines a simple example to analyze the preg_match_all in PHP to obtain the content of the P element and img src element in the HTML tag. Friends in need can refer to it
<?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; ?>
The running results are as follows:
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 ) ) )
##Related recommendations:
PHPRegular matchingInstance code of date and time (time stamp conversion)
Regular matchingCauses and solutions for failure of all characters
##PHP simple implementation
The above is the detailed content of How to implement regular matching operation in PHP. For more information, please follow other related articles on the PHP Chinese website!