Created
December 20, 2023 09:55
-
-
Save pdewouters/70af7a3473b7f30ca4d91286ae5370b4 to your computer and use it in GitHub Desktop.
count html tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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