Skip to content

Instantly share code, notes, and snippets.

@pdewouters
Created December 20, 2023 09:55
Show Gist options
  • Save pdewouters/70af7a3473b7f30ca4d91286ae5370b4 to your computer and use it in GitHub Desktop.
Save pdewouters/70af7a3473b7f30ca4d91286ae5370b4 to your computer and use it in GitHub Desktop.
count html tags
function count_tags( string $html ) {
// save the tag count to an option so each call to this function use it for a global count across all posts.
$tag_count = get_option( 'tibi_tag_count', [] );
$tags = new \WP_HTML_Tag_Processor( $html );
while( $tags->next_tag()) {
$tag = $tags->get_tag();
if ( ! isset( $tag_count[ $tag ] ) ) {
$tag_count[ $tag ] = 0;
}
$tag_count[ $tag ]++;
}
update_option( 'tibi_tag_count', $tag_count );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment