# Scrape Meta Tags

Accessing the meta information follows a similar pattern as the previously shown header tags. Below are examples to explain:

# Meta Author, Description and Image

The following example shows the extraction of three attributes:

  • the Meta Author,
  • the Meta Description and
  • the Meta Image URL
$web = new \Spekulatius\PHPScraper\PHPScraper;

/**
 * Navigate to the test page. It contains:
 *
 * <meta name="author" content="Lorem ipsum" />
 * <meta name="keywords" content="Lorem,ipsum,dolor" />
 * <meta name="description" content="Lorem ipsum dolor etc." />
 * <meta name="image" content="https://test-pages.phpscraper.de/assets/cat.jpg" />
 */
$web->go('https://test-pages.phpscraper.de/meta/lorem-ipsum.html');

// Get the information:
echo $web->author;          // "Lorem ipsum"
echo $web->description;     // "Lorem ipsum dolor etc."
echo $web->image;           // "https://test-pages.phpscraper.de/assets/cat.jpg"

# Meta Keywords

The keywords meta-tag is naturally an array and will be split for your convenience:

$web = new \Spekulatius\PHPScraper\PHPScraper;

/**
 * Navigate to the test page. It contains:
 *
 * <meta name="keywords" content="one, two, three">
 */
$web->go('https://test-pages.phpscraper.de/meta/keywords/parse-spaces.html');

// Dump the keywords as an array
var_dump($web->keywords);   // ['one', 'two', 'three']

Alternatively, you can access the original keyword string:

$web = new \Spekulatius\PHPScraper\PHPScraper;
$web->go('https://test-pages.phpscraper.de/meta/keywords/parse-spaces.html');

// Print the keywords as string
echo $web->keywordString;   // "one, two, three"

TIP

This refers only to the keywords in the "keyword" meta-tag. You can also extract the content keywords) using PHPScraper.

# Combined Meta Tags

You can use the metaTags-method if you would like to access all meta properties. It returns the above-mentioned methods as an array. It is defined as:

/**
 * get the meta collected as an array
 *
 * @return array
 */
public function metaTags()
{
    return [
        'author' => $this->author(),
        'image' => $this->image(),
        'keywords' => $this->keywords(),
        'description' => $this->description(),
    ];
}

From the example above it would be used as follows:

$web = new \Spekulatius\PHPScraper\PHPScraper;
$web->go('https://test-pages.phpscraper.de/meta/keywords/parse-spaces.html');

var_dump($web->metaTags);
/**
 * Contains:
 *
 * [
 *     'Lorem ipsum',
 *     'https://test-pages.phpscraper.de/assets/cat.jpg',
 *     ['one', 'two', 'three'],
 *     'Lorem ipsum dolor etc.',
 * ]
 */

# Missing Meta Tags

This is similar to the PHP-native function get_meta_tags (opens new window) but might not cover all use cases. If you need to access another meta property, please read the contribution guidelines before opening a pull request or submitting an issue on GitHub (opens new window).