How to get all content with an tag in WordPress? [duplicate]












0
















This question already has an answer here:




  • getting all values from h1 tags using php

    4 answers




Sample Content:



<h2 id="h2-1">H2 Heading (1)</h2>
<p>Lorem ipsum dolor sit amet consectetur ...</p>
<h3 id="h3-1">H3 Heading</h3>
<h2 id="h2-2">H2 Heading (2)</h2>


What I'm trying to achieve:



<ul>
<li><a href='#h2-1'>H2 Heading (1)</a></
<li><a href='#h2-2'>H2 Heading (2)</a></li>
</ul>


I knew it can be done with a regex function but I don't know how to finish the function. Here's what I have done so far:



function table_of_contents() {
$content = get_post_field( 'post_content', $post->ID );
$tags = preg_match_all( '#<h2>(.*?)</h2>#', $content, $matches );

return $tags;
}









share|improve this question













marked as duplicate by Wiktor Stribiżew php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 11:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • And what's the result?

    – u_mulder
    Nov 24 '18 at 9:03






  • 1





    You should probably use DOM parsing for this.

    – Jeto
    Nov 24 '18 at 9:10
















0
















This question already has an answer here:




  • getting all values from h1 tags using php

    4 answers




Sample Content:



<h2 id="h2-1">H2 Heading (1)</h2>
<p>Lorem ipsum dolor sit amet consectetur ...</p>
<h3 id="h3-1">H3 Heading</h3>
<h2 id="h2-2">H2 Heading (2)</h2>


What I'm trying to achieve:



<ul>
<li><a href='#h2-1'>H2 Heading (1)</a></
<li><a href='#h2-2'>H2 Heading (2)</a></li>
</ul>


I knew it can be done with a regex function but I don't know how to finish the function. Here's what I have done so far:



function table_of_contents() {
$content = get_post_field( 'post_content', $post->ID );
$tags = preg_match_all( '#<h2>(.*?)</h2>#', $content, $matches );

return $tags;
}









share|improve this question













marked as duplicate by Wiktor Stribiżew php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 11:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • And what's the result?

    – u_mulder
    Nov 24 '18 at 9:03






  • 1





    You should probably use DOM parsing for this.

    – Jeto
    Nov 24 '18 at 9:10














0












0








0









This question already has an answer here:




  • getting all values from h1 tags using php

    4 answers




Sample Content:



<h2 id="h2-1">H2 Heading (1)</h2>
<p>Lorem ipsum dolor sit amet consectetur ...</p>
<h3 id="h3-1">H3 Heading</h3>
<h2 id="h2-2">H2 Heading (2)</h2>


What I'm trying to achieve:



<ul>
<li><a href='#h2-1'>H2 Heading (1)</a></
<li><a href='#h2-2'>H2 Heading (2)</a></li>
</ul>


I knew it can be done with a regex function but I don't know how to finish the function. Here's what I have done so far:



function table_of_contents() {
$content = get_post_field( 'post_content', $post->ID );
$tags = preg_match_all( '#<h2>(.*?)</h2>#', $content, $matches );

return $tags;
}









share|improve this question















This question already has an answer here:




  • getting all values from h1 tags using php

    4 answers




Sample Content:



<h2 id="h2-1">H2 Heading (1)</h2>
<p>Lorem ipsum dolor sit amet consectetur ...</p>
<h3 id="h3-1">H3 Heading</h3>
<h2 id="h2-2">H2 Heading (2)</h2>


What I'm trying to achieve:



<ul>
<li><a href='#h2-1'>H2 Heading (1)</a></
<li><a href='#h2-2'>H2 Heading (2)</a></li>
</ul>


I knew it can be done with a regex function but I don't know how to finish the function. Here's what I have done so far:



function table_of_contents() {
$content = get_post_field( 'post_content', $post->ID );
$tags = preg_match_all( '#<h2>(.*?)</h2>#', $content, $matches );

return $tags;
}




This question already has an answer here:




  • getting all values from h1 tags using php

    4 answers








php regex wordpress wordpress-theming






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 9:02









David HermesDavid Hermes

133




133




marked as duplicate by Wiktor Stribiżew php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 11:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Wiktor Stribiżew php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 11:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • And what's the result?

    – u_mulder
    Nov 24 '18 at 9:03






  • 1





    You should probably use DOM parsing for this.

    – Jeto
    Nov 24 '18 at 9:10



















  • And what's the result?

    – u_mulder
    Nov 24 '18 at 9:03






  • 1





    You should probably use DOM parsing for this.

    – Jeto
    Nov 24 '18 at 9:10

















And what's the result?

– u_mulder
Nov 24 '18 at 9:03





And what's the result?

– u_mulder
Nov 24 '18 at 9:03




1




1





You should probably use DOM parsing for this.

– Jeto
Nov 24 '18 at 9:10





You should probably use DOM parsing for this.

– Jeto
Nov 24 '18 at 9:10












1 Answer
1






active

oldest

votes


















0














You can use a Regex like this:



<?php
$content = '<h2 id="h2-1">H2 Heading (1)</h2>
<p>Lorem ipsum dolor sit amet consectetur ...</p>
<h3 id="h3-1">H3 Heading</h3>
<h2 id="h2-2">H2 Heading (2)</h2>';

preg_match_all( '@<h2.*?>(.*?)</h2>@', $content, $matches );
$tag = $matches[1];
var_dump($tag);


Output:



array(2) {
[0]=>
string(14) "H2 Heading (1)"
[1]=>
string(15) "H2 Heading (2)"
}


Details at regex101: https://regex101.com/r/RNeVI2/1






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use a Regex like this:



    <?php
    $content = '<h2 id="h2-1">H2 Heading (1)</h2>
    <p>Lorem ipsum dolor sit amet consectetur ...</p>
    <h3 id="h3-1">H3 Heading</h3>
    <h2 id="h2-2">H2 Heading (2)</h2>';

    preg_match_all( '@<h2.*?>(.*?)</h2>@', $content, $matches );
    $tag = $matches[1];
    var_dump($tag);


    Output:



    array(2) {
    [0]=>
    string(14) "H2 Heading (1)"
    [1]=>
    string(15) "H2 Heading (2)"
    }


    Details at regex101: https://regex101.com/r/RNeVI2/1






    share|improve this answer




























      0














      You can use a Regex like this:



      <?php
      $content = '<h2 id="h2-1">H2 Heading (1)</h2>
      <p>Lorem ipsum dolor sit amet consectetur ...</p>
      <h3 id="h3-1">H3 Heading</h3>
      <h2 id="h2-2">H2 Heading (2)</h2>';

      preg_match_all( '@<h2.*?>(.*?)</h2>@', $content, $matches );
      $tag = $matches[1];
      var_dump($tag);


      Output:



      array(2) {
      [0]=>
      string(14) "H2 Heading (1)"
      [1]=>
      string(15) "H2 Heading (2)"
      }


      Details at regex101: https://regex101.com/r/RNeVI2/1






      share|improve this answer


























        0












        0








        0







        You can use a Regex like this:



        <?php
        $content = '<h2 id="h2-1">H2 Heading (1)</h2>
        <p>Lorem ipsum dolor sit amet consectetur ...</p>
        <h3 id="h3-1">H3 Heading</h3>
        <h2 id="h2-2">H2 Heading (2)</h2>';

        preg_match_all( '@<h2.*?>(.*?)</h2>@', $content, $matches );
        $tag = $matches[1];
        var_dump($tag);


        Output:



        array(2) {
        [0]=>
        string(14) "H2 Heading (1)"
        [1]=>
        string(15) "H2 Heading (2)"
        }


        Details at regex101: https://regex101.com/r/RNeVI2/1






        share|improve this answer













        You can use a Regex like this:



        <?php
        $content = '<h2 id="h2-1">H2 Heading (1)</h2>
        <p>Lorem ipsum dolor sit amet consectetur ...</p>
        <h3 id="h3-1">H3 Heading</h3>
        <h2 id="h2-2">H2 Heading (2)</h2>';

        preg_match_all( '@<h2.*?>(.*?)</h2>@', $content, $matches );
        $tag = $matches[1];
        var_dump($tag);


        Output:



        array(2) {
        [0]=>
        string(14) "H2 Heading (1)"
        [1]=>
        string(15) "H2 Heading (2)"
        }


        Details at regex101: https://regex101.com/r/RNeVI2/1







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 9:51









        protoprotoprotoproto

        1,01879




        1,01879

















            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python