fix: use preg_match with capture groups for nbsp publisher stripping

Previous strrpos-based approach wasn't catching all cases. Switch to a
preg_match with /u flag and capture groups to reliably split text at
nbsp separator boundaries.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Terence Ong
2026-04-08 10:53:53 +07:00
parent aeceab2ae4
commit 0a550cabfe
2 changed files with 8 additions and 30 deletions

View File

@@ -248,21 +248,10 @@ class Peptide_News_Rest_API {
// Fallback 2: strip trailing nbsp-separated publisher names.
// Google News RSS excerpts use double non-breaking space + publisher name
// when the source field doesn't match (e.g., source = "news.google.com").
// Uses string search instead of regex to avoid UTF-8/PCRE edge cases.
$double_nbsp = $nbsp . $nbsp;
$nbsp_pos = strrpos( $text, $double_nbsp );
if ( false !== $nbsp_pos ) {
$after = substr( $text, $nbsp_pos + strlen( $double_nbsp ) );
} else {
$nbsp_pos = strrpos( $text, $nbsp );
if ( false !== $nbsp_pos ) {
$after = substr( $text, $nbsp_pos + strlen( $nbsp ) );
}
}
if ( false !== $nbsp_pos && $nbsp_pos > strlen( $text ) * 0.4 ) {
// Only strip if what follows looks like a publisher name (< 60 chars, starts uppercase).
if ( ! empty( $after ) && strlen( $after ) < 60 && preg_match( '/^[A-Z]/u', $after ) ) {
return rtrim( substr( $text, 0, $nbsp_pos ) );
// Uses mb_ereg for reliable multibyte matching.
if ( preg_match( '/^(.{10,}?)\x{00A0}{1,2}([A-Z][^.!?\x{00A0}]{0,55})$/u', $text, $m ) ) {
if ( mb_strlen( $m[1], 'UTF-8' ) > mb_strlen( $text, 'UTF-8' ) * 0.4 ) {
return rtrim( $m[1] );
}
}

View File

@@ -242,21 +242,10 @@ class Peptide_News_Rest_API {
// Fallback 2: strip trailing nbsp-separated publisher names.
// Google News RSS excerpts use double non-breaking space + publisher name
// when the source field doesn't match (e.g., source = "news.google.com").
// Uses string search instead of regex to avoid UTF-8/PCRE edge cases.
$double_nbsp = $nbsp . $nbsp;
$nbsp_pos = strrpos( $text, $double_nbsp );
if ( false !== $nbsp_pos ) {
$after = substr( $text, $nbsp_pos + strlen( $double_nbsp ) );
} else {
$nbsp_pos = strrpos( $text, $nbsp );
if ( false !== $nbsp_pos ) {
$after = substr( $text, $nbsp_pos + strlen( $nbsp ) );
}
}
if ( false !== $nbsp_pos && $nbsp_pos > strlen( $text ) * 0.4 ) {
// Only strip if what follows looks like a publisher name (< 60 chars, starts uppercase).
if ( ! empty( $after ) && strlen( $after ) < 60 && preg_match( '/^[A-Z]/u', $after ) ) {
return rtrim( substr( $text, 0, $nbsp_pos ) );
// Uses mb_ereg for reliable multibyte matching.
if ( preg_match( '/^(.{10,}?)\x{00A0}{1,2}([A-Z][^.!?\x{00A0}]{0,55})$/u', $text, $m ) ) {
if ( mb_strlen( $m[1], 'UTF-8' ) > mb_strlen( $text, 'UTF-8' ) * 0.4 ) {
return rtrim( $m[1] );
}
}