Home  >  Article  >  Backend Development  >  php regular expression example

php regular expression example

怪我咯
怪我咯Original
2017-07-14 14:27:421427browse

Regular expression(regular expression) describes a stringmatching pattern (pattern), which can be used to check whether a string contains a certain substring and match the Substring replacement or removing a substring that meets a certain condition from a certain string, etc.

Regular expressions are text patterns composed of ordinary characters (such as the characters a through z) and special characters (called "metacharacters"). A pattern describes one or more strings to match when searching for text. A regular expression acts as a template that matches a character pattern with a searched string.

Example

<?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;
?>

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
        )
    )
)

The above is the detailed content of php regular expression example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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