# Scrape Headings

Heading can be useful to get an idea of the content on a website. The following example shows how to scrape:

  • A single Heading
  • All headings of a particular level (e.g. <h3>)
  • All headings on a page

# Scrape Single Heading

Scraping a single heading is easy and be done following this example:

$web = new \Spekulatius\PHPScraper\PHPScraper;

/**
 * Navigate to the test page. It contains:
 *
 * <title>Outline Test</title>
 */
$web->go('https://test-pages.phpscraper.de/content/online.html');

// Print the H1 heading
echo $web->h1[0];          // "Outline Test"

TIP

The website title and heading 1 (<h1>) can be different. Make sure you retrieve the correct one.

# Headings by Level

There might be cases, in which all headings of a particular level should be retrieved. The example below shows how to do so:

$web = new \Spekulatius\PHPScraper\PHPScraper;

/**
 * Navigate to the test page. It contains:
 *
 * <h3>Example 1</h3>
 * <p>Here would be an example.</p>
 *
 * <h3>Example 2</h3>
 * <p>Here would be the second example.</p>
 *
 * <h3>Example 3</h3>
 * <p>Here would be another example.</p>
 */
$web->go('https://test-pages.phpscraper.de/content/online.html');

/**
 * Return the h3 headings:
 *
 * [
 *    'Example 1',
 *    'Example 2',
 *    'Example 3'
 * ]
 */
$web->h3;

If no headings are found, the array is left empty.

# All Headings on a Page

To access all headings on a page, you can do so by accessing the different levels from 1 to 6. Or, alternatively, you can access all at once:

$web = new \Spekulatius\PHPScraper\PHPScraper;

/**
 * Navigate to the test page. This page contains:
 *
 * <h1>We are testing here!</h1>
 * <p>This page contains an example structure to be parsed. It comes with a number of headings and nested paragraphs as an scrape example.</p>
 *
 * <h2>Examples</h2>
 * <p>There are numerous examples on the website. Please check them out to get more context on how scraping works.</p>
 *
 * <h3>Example 1</h3>
 * <p>Here would be an example.</p>
 *
 * <h3>Example 2</h3>
 * <p>Here would be the second example.</p>
 *
 * <h3>Example 3</h3>
 * <p>Here would be another example.</p>
 */
$web->go('https://test-pages.phpscraper.de/content/online.html');

/**
 * $headings contains now:
 *
 * [
 *     [
 *         'We are testing here!'
 *     ],
 *     [
 *         'Examples'
 *     ],
 *     [
 *         'Example 1',
 *         'Example 2',
 *         'Example 3',
 *     ],
 *     [],
 *     [],
 *     []
 * ]
 */
$web->headings;

As you can see, this doesn't contain any information about the structure the headings are in. It's purely to know which headings exist. If you like to have an outline you will need use the related methods.