-1

How to impliment an OR here to match meta with both name and property

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

output

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

)
TheMonkeyKing
  • 338
  • 2
  • 9
  • You should use a parser but there is an `or` in regex. Have you looked at any docs? http://www.regular-expressions.info/alternation.html – chris85 Dec 30 '16 at 23:13
  • @chris85 prefer parser than regex ? – TheMonkeyKing Dec 30 '16 at 23:15
  • Yes, with a parser you don't need to worry about whitespace, order of attributes, encapsulation type of attribute values, etc. If this is just a regex question I think my previous link should answer that. HTML/XML aren't meant to be processed with regexs though. – chris85 Dec 30 '16 at 23:16
  • Then I should try some thing like simple_html_doom_parser? – TheMonkeyKing Dec 30 '16 at 23:20
  • You should use jQuery. It's really great and does all things. – MD XF Dec 31 '16 at 01:29

1 Answers1

0

As learned from the suggested link by @chris85, (name|property) will do this. My bad!

preg_match_all('/<[\s]*meta[\s]*(name|property)="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $html, $match);
TheMonkeyKing
  • 338
  • 2
  • 9
  • 1
    If you dont need the `name`/`property` captured you can make it a non capturing group with `?:`. e.g. `(?:name|property)` – chris85 Dec 31 '16 at 16:05