'',
'content' => '',
'excerpt' => '',
);
}
// Suppress warnings from malformed HTML.
libxml_use_internal_errors( true );
$dom = new DOMDocument();
// Ensure UTF-8 handling.
$dom->loadHTML( '' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
libxml_clear_errors();
$xpath = new DOMXPath( $dom );
// 1. Extract
.
$title = '';
$title_nodes = $xpath->query( '//title' );
if ( $title_nodes && $title_nodes->length > 0 ) {
$title = trim( $title_nodes->item( 0 )->textContent );
$title = html_entity_decode( $title, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
}
// 2. Remove unwanted tags (scripts, styles, navigation, headers, footers).
$remove_tags = array( 'script', 'style', 'noscript', 'nav', 'header', 'footer', 'aside', 'iframe', 'svg', 'canvas' );
foreach ( $remove_tags as $tag ) {
$nodes = $dom->getElementsByTagName( $tag );
$to_remove = array();
foreach ( $nodes as $node ) {
$to_remove[] = $node;
}
foreach ( $to_remove as $node ) {
if ( $node->parentNode ) {
$node->parentNode->removeChild( $node );
}
}
}
// 3. Extract text from body or whole document.
$body_nodes = $dom->getElementsByTagName( 'body' );
$target_node = ( $body_nodes && $body_nodes->length > 0 ) ? $body_nodes->item( 0 ) : $dom->documentElement;
$content_text = '';
if ( $target_node ) {
$content_text = self::extract_structured_text( $target_node );
}
// Normalize whitespace while preserving paragraph breaks.
$content_text = preg_replace( "/[ \t]+/", ' ', $content_text );
$content_text = preg_replace( "/(\r?\n){3,}/", "\n\n", $content_text );
$content_text = trim( $content_text );
// 4. Generate excerpt (~30 words, clean word boundary per Question 14).
$excerpt = self::generate_excerpt( $content_text, 30 );
return array(
'title' => sanitize_text_field( $title ),
'content' => $content_text,
'excerpt' => $excerpt,
);
}
/**
* Recursively extracts text with paragraph and heading breaks.
*
* @param \DOMNode $node
* @return string
*/
private static function extract_structured_text( $node ) {
$text = '';
if ( XML_TEXT_NODE === $node->nodeType ) {
return $node->nodeValue;
}
$block_elements = array(
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'p', 'div', 'article', 'section', 'li', 'blockquote', 'tr'
);
$tag = strtolower( $node->nodeName );
$is_block = in_array( $tag, $block_elements, true );
foreach ( $node->childNodes as $child ) {
$text .= self::extract_structured_text( $child );
}
if ( $is_block ) {
$text = "\n\n" . trim( $text ) . "\n\n";
} elseif ( 'br' === $tag ) {
$text .= "\n";
} elseif ( 'td' === $tag || 'th' === $tag ) {
$text .= " ";
}
return $text;
}
/**
* Generates a clean plain text excerpt of specified word count.
*
* @param string $text
* @param int $word_count
* @return string
*/
public static function generate_excerpt( $text, $word_count = 30 ) {
$clean = wp_strip_all_tags( $text );
$clean = preg_replace( '/\s+/', ' ', $clean );
$clean = trim( $clean );
$words = explode( ' ', $clean );
if ( count( $words ) <= $word_count ) {
return $clean;
}
$sliced = array_slice( $words, 0, $word_count );
return rtrim( implode( ' ', $sliced ), '.,;:!' ) . '...';
}
}