Getting All Values from H1 Tags Using PHP

getting all values from h1 tags using php

you could use simplehtmldom:

function getTextBetweenTags($string, $tagname) {
// Create DOM from string
$html = str_get_html($string);

$titles = array();
// Find all tags
foreach($html->find($tagname) as $element) {
$titles[] = $element->plaintext;

How can I show only h1 tags in The Loop?

You can parse all the H1s out of the post content with domDocument:

if($hm_intro->have_posts()) : while($hm_intro->have_posts()) : $hm_intro->the_post();

$dom = new domDocument;

$headings = $dom->getElementsByTagName('h1');

foreach ($heading as $h){
echo '<h1>' . $h->textContent . '</h1>';


How to extract Heading tags in PHP from a string?

If you actually want to use regular expressions, I think that:

preg_match_all('/<h[0-6]>([^</h[0-6]>*)</h/i', $string, $matches);

should work as long as your header tags are not nested. As others have said, if you're not in control of the HTML, regular expressions are not a great way to do this.

Xpath - get text from all h1, h3 p tags within a div

Why don't you try

$xpath->query("//div[contains(concat(' ', normalize-space(@class), ' '), ' grid_9 alpha omega newscontainer arena ')]/*[local-name()='h1' or local-name()='p' or local-name()='h3']");

This should give you the nodes in the order of their appearance restricted to children of the div parent and also in XPath 1.0 which I assume is an unmentioned prerequisite.

Related Topics

Leave a reply