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;
$dom->loadHTML(get_the_content());
$headings = $dom->getElementsByTagName('h1');
foreach ($heading as $h){
echo '<h1>' . $h->textContent . '</h1>';
}
endwhile;
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
What Is the Syntax for Sorting an Eloquent Collection by Multiple Columns
Make a Path Work Both on Linux and Windows
Download a File from Ftp Using Curl and PHP
Remotely Destroy a Session in PHP (User Logs in Somewhere Else)
Twig Iterate Over Object Properties
Unexpected T_Encapsed_And_Whitespace, Expecting T_String or T_Variable or T_Num_String Error
Reflectionexception: Class Classname Does Not Exist - Laravel
PHP Strtotime "Last Monday" If Today Is Monday
Change Foreign Characters to Their Roman Equivalent
PHP Post_Max_Size VS Upload_Max_Filesize, What Is the Difference
PHP Preg_Match (.*) Not Matching Past Line Breaks
Colorizing Windows Command Line Output from PHP
How to Manage a Single PHP5 Session on Multiple Apache Servers
Select Random File from Directory