当前位置:   article > 正文

PHP preg_match_all 获取html中固定的标签内容_php获取html标签内容

php获取html标签内容

一、前言

我们需要获取一段html中固定的标签内容,用PHP中的preg_match_all方法匹配

二、内容

  • 我们需要获取 a 标签中href属性的值,则代码如下
    public function getContent() {
        $str = <<<EOF
        <a href="http://xxx1.com">合抱之木</a>
        <a href="http://xxx2.com">生于毫末</a>
        <a href="http://xxx3.com">九层之台</a>
        <a href="http://xxx4.com">起于累土</a>
        <a href="http://xxx5.com">千里之行</a>
        <a href="http://xxx6.com">始于足下</a>
EOF;

        preg_match_all('/href="([^"]+)/', $str, $match);    //匹配 href="" 里面的内容

        echo '<pre>';
        var_export($match);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

三、打印

array (
  0 => 
  array (
    0 => 'href="http://xxx1.com',
    1 => 'href="http://xxx2.com',
    2 => 'href="http://xxx3.com',
    3 => 'href="http://xxx4.com',
    4 => 'href="http://xxx5.com',
    5 => 'href="http://xxx6.com',
  ),
  1 => 
  array (
    0 => 'http://xxx1.com',
    1 => 'http://xxx2.com',
    2 => 'http://xxx3.com',
    3 => 'http://xxx4.com',
    4 => 'http://xxx5.com',
    5 => 'http://xxx6.com',
  ),
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/197733
推荐阅读
相关标签
  

闽ICP备14008679号