How to get all content with an tag in WordPress? [duplicate]
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;
}
php regex wordpress wordpress-theming
marked as duplicate by Wiktor Stribiżew
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.
add a comment |
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;
}
php regex wordpress wordpress-theming
marked as duplicate by Wiktor Stribiżew
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
add a comment |
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;
}
php regex wordpress wordpress-theming
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
php regex wordpress wordpress-theming
asked Nov 24 '18 at 9:02
David HermesDavid Hermes
133
133
marked as duplicate by Wiktor Stribiżew
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
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Nov 24 '18 at 9:51
protoprotoprotoproto
1,01879
1,01879
add a comment |
add a comment |
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