Home >Backend Development >PHP Tutorial > 匹配html格式的正则!

匹配html格式的正则!

WBOY
WBOYOriginal
2016-06-13 12:58:57817browse

求一个匹配html格式的正则!!

<br />
<ul id="ul1"><br />
  <li>1</li><br />
  <li>2</li><br />
</ul><br />

请问可以用正则匹配出来这样一段HTML代码吗?我不会写正则,去网上看了点基础教程,匹配出来的结果只有

     里面的
  • 匹配不到(里面的li是不定数量的,可以能是2个,也会是更多~)。求高手帮忙写下正则!
    ------解决方案--------------------
    <?php<br />
    <br />
    $str = 'afdasdfdsa<ul id="ul1"><br />
      <li>1</li><br />
      <li>2</li><br />
    </ul>asdfasdfasd<ul id="ul2"><br />
      <li>3</li><br />
      <li>4</li><br />
    </ul>fdsgsdfg';<br />
    <br />
    preg_match_all('/<ul[^>]*>.*<\/ul>/isU', $str, $matches);<br />
    print_r($matches[0]);<br />
    <br />
    ?>

    ------解决方案--------------------
    <?php  <br />
    $str = '<ul id="ul1"><br />
      <li>1</li><br />
      <li>2</li><br />
    </ul><br />
    <ul id="ul1"><br />
      <li>3</li><br />
    </ul><br />
    <ul id="ul1"><br />
      <li>4</li><br />
      <li>5</li><br />
      <li>6</li><br />
    </ul>';<br />
    preg_match_all('/<ul.*?>.*?<\/ul>/s', $str, $matches);<br />
    var_dump($matches);
  • 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