Skip to content

Instantly share code, notes, and snippets.

@lucdkny
Created October 5, 2016 02:59
Show Gist options
  • Save lucdkny/acebab4c2d5425f86eca0a6fd452ef54 to your computer and use it in GitHub Desktop.
Save lucdkny/acebab4c2d5425f86eca0a6fd452ef54 to your computer and use it in GitHub Desktop.

Revisions

  1. @jeremiahlee jeremiahlee created this gist Jan 19, 2011.
    21 changes: 21 additions & 0 deletions php-regex-meta-description.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    <?php
    function parseDescription($html) {
    // Get the 'content' attribute value in a <meta name="description" ... />
    $matches = array();

    // Search for <meta name="description" content="Buy my stuff" />
    preg_match('/<meta.*?name=("|\')description("|\').*?content=("|\')(.*?)("|\')/i', $html, $matches);
    if (count($matches) > 4) {
    return trim($matches[4]);
    }

    // Order of attributes could be swapped around: <meta content="Buy my stuff" name="description" />
    preg_match('/<meta.*?content=("|\')(.*?)("|\').*?name=("|\')description("|\')/i', $html, $matches);
    if (count($matches) > 2) {
    return trim($matches[2]);
    }

    // No match
    return null;
    }
    ?>