preg_match元名称OR属性

时间:2016-12-30 23:09:11

标签: php regex preg-match preg-match-all

如何在此处添加OR以使metanameproperty匹配$html = '<meta name="title" content="title here.."> <meta name="keywords" content="keywords here.."> <meta property="og:title" content="og title here..">' preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $html, $match);

Array
(
    [0] => Array
        (
            [0] => <meta name="title" content="title here..">
            [1] => <meta name="keywords" content="keywords here..">
         // [2] <meta property="og:title" content="og title here..">
        )

    [1] => Array
        (
            [0] => title
            [1] => keywords
         // [2] => og:title
        )

    [2] => Array
        (
            [0] => title here..
            [1] => keywords here..
         // [2] => og title here..
        )

)

输出

{{1}}

1 个答案:

答案 0 :(得分:0)

从@ chris85建议的链接中了解到,(name|property)将会这样做。我的坏!

preg_match_all('/<[\s]*meta[\s]*(name|property)="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $html, $match);