Home > Backend Development > PHP Tutorial > Simple operation method of PHP regular matching

Simple operation method of PHP regular matching

小云云
Release: 2023-03-20 06:26:01
Original
1351 people have browsed it

This article mainly introduces the PHP regular matching operation to you, and analyzes the preg_match_all in PHP's preg_match_all acquisition skills for the content of the P element and img src element in the HTML tag with a simple example. Friends who need it can refer to it. I hope it can help everyone.


<?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(&#39;%<p.*?>(.*?)</p>%si&#39;, $str, $matches)) {
  $arr[0][] = $matches[1];
}
if(preg_match_all(&#39;/src="([^<]*)" >/i&#39;, $str, $matches)) {
  $arr[1][] = $matches[1];
}
print_r($arr);
exit;
?>
Copy after login

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
        )
    )
)
Copy after login

Related recommendations:

PHP regular match method for Chinese and English, numbers and underlines

php regular match the remote image address in the article and download the image to the local

php Regular match specifies the beginning and end of the content & non-greedy mode

The above is the detailed content of Simple operation method of PHP regular matching. For more information, please follow other related articles on the PHP Chinese website!

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