*/
private static array $wordsMemo = [];
/**
* Reset in-memory static caches (primarily for unit tests and long-lived processes).
*
* @return void
*/
public static function resetCache(): void {
self::$wordsMemo = [];
}
/**
* Strip tags and normalise whitespace, for word and sentence counting.
*
* @param string $html Raw HTML or plain text.
* @return string Plain text.
*/
public static function plainText( string $html ): string {
$text = (string) preg_replace( '/<(script|style)\b[^>]*>.*?<\/\1>/is', ' ', $html );
$text = (string) preg_replace( '/
/i', "\n", $text );
$text = (string) preg_replace( '/<\/(p|div|h[1-6]|li|blockquote|tr)>/i', "\n", $text );
// Any tag still standing after the pass above is removed here. Doing this
// with a pattern rather than wp_strip_all_tags keeps the class free of a
// function that callers guard with function_exists, which would otherwise
// make the analyser depend on the harness rather than the other way round.
$text = (string) preg_replace( '/<[^>]*>/', ' ', $text );
$text = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
$text = (string) preg_replace( '/[ \t\x0B\f\r]+/', ' ', $text );
$text = (string) preg_replace( '/\n\s*\n+/', "\n", $text );
return trim( $text );
}
/**
* Split text into words.
*
* @param string $text Plain text.
* @return string[] Words, punctuation removed, empties dropped.
*/
public static function words( string $text ): array {
if ( array_key_exists( $text, self::$wordsMemo ) ) {
return self::$wordsMemo[ $text ];
}
$parts = preg_split( '/[^\p{L}\p{N}\'’-]+/u', $text );
if ( ! is_array( $parts ) ) {
self::$wordsMemo[ $text ] = [];
return [];
}
$words = [];
foreach ( $parts as $part ) {
$part = trim( $part, "'’-" );
if ( '' !== $part ) {
$words[] = $part;
}
}
self::$wordsMemo[ $text ] = $words;
return $words;
}
/**
* Count words in plain text.
*
* @param string $text Plain text.
* @return int The result.
*/
public static function wordCount( string $text ): int {
return count( self::words( $text ) );
}
/**
* Split text into sentences.
*
* Newlines also end a sentence, so a list without punctuation does not
* merge into one enormous sentence and skew every ratio.
*
* @param string $text Plain text.
* @return string[] Sentences, trimmed, empties dropped.
*/
public static function sentences( string $text ): array {
$sentences = [];
foreach ( explode( "\n", $text ) as $line ) {
$line = trim( $line );
if ( '' === $line ) {
continue;
}
$parts = preg_split( '/(?<=[' . preg_quote( self::SENTENCE_ENDINGS, '/' ) . '])\s+/u', $line );
if ( ! is_array( $parts ) ) {
continue;
}
foreach ( $parts as $part ) {
$part = trim( $part );
if ( '' !== $part ) {
$sentences[] = $part;
}
}
}
return $sentences;
}
/**
* Split text into paragraphs.
*
* @param string $text Plain text.
* @return string[] Paragraphs, trimmed, empties dropped.
*/
public static function paragraphs( string $text ): array {
$paragraphs = [];
foreach ( preg_split( '/\n+/', $text ) as $part ) {
$part = trim( (string) $part );
if ( '' !== $part ) {
$paragraphs[] = $part;
}
}
return $paragraphs;
}
/**
* Extract heading levels and text.
*
* @param string $html Raw HTML.
* @return array Headings in order.
*/
public static function headings( string $html ): array {
if ( ! preg_match_all( '/]*>(.*?)<\/h\1>/is', $html, $matches, PREG_SET_ORDER ) ) {
return [];
}
$headings = [];
foreach ( $matches as $match ) {
$headings[] = [
'level' => (int) $match[1],
'text' => self::plainText( (string) $match[2] ),
];
}
return $headings;
}
/**
* Extract image alt values.
*
* @param string $html Raw HTML.
* @return string[] Alt attributes, including empty ones.
*/
public static function imageAlts( string $html ): array {
if ( ! preg_match_all( '/
]*>/i', $html, $matches ) ) {
return [];
}
$alts = [];
foreach ( $matches[0] as $tag ) {
$alts[] = preg_match( '/\balt\s*=\s*("([^"]*)"|\'([^\']*)\')/i', (string) $tag, $alt )
? (string) ( '' !== ( $alt[2] ?? '' ) ? $alt[2] : ( $alt[3] ?? '' ) )
: '';
}
return $alts;
}
/**
* Extract anchors with their href, rel and anchor text.
*
* Classification into internal and external is left to the caller, which
* knows the site URL.
*
* @param string $html Raw HTML.
* @return array Anchors in order.
*/
public static function links( string $html ): array {
if ( ! preg_match_all( '/]*)>(.*?)<\/a>/is', $html, $matches, PREG_SET_ORDER ) ) {
return [];
}
$links = [];
foreach ( $matches as $match ) {
$attributes = (string) $match[1];
$href = preg_match( '/\bhref\s*=\s*("([^"]*)"|\'([^\']*)\')/i', $attributes, $found )
? (string) ( '' !== ( $found[2] ?? '' ) ? $found[2] : ( $found[3] ?? '' ) )
: '';
$rel = preg_match( '/\brel\s*=\s*("([^"]*)"|\'([^\']*)\')/i', $attributes, $relFound )
? (string) ( '' !== ( $relFound[2] ?? '' ) ? $relFound[2] : ( $relFound[3] ?? '' ) )
: '';
$links[] = [
'href' => html_entity_decode( $href, ENT_QUOTES | ENT_HTML5, 'UTF-8' ),
'rel' => strtolower( $rel ),
'text' => self::plainText( (string) $match[2] ),
];
}
return $links;
}
/**
* Count the media a reader can see.
*
* @param string $html Raw HTML.
* @return array{images: int, videos: int} Media counts.
*/
public static function media( string $html ): array {
$images = preg_match_all( '/
]*>/i', $html );
$images = ( false === $images ) ? 0 : $images;
$galleries = preg_match_all( '/\[gallery\b[^]]*\]/i', $html );
$images += ( false === $galleries ) ? 0 : $galleries;
$videos = preg_match_all( '/