Fix EntityRef: expecting ; PHP error when reading public RSS feeds
15845
post-template-default,single,single-post,postid-15845,single-format-standard,bridge-core-3.0.1,qodef-qi--no-touch,qi-addons-for-elementor-1.5.3,qode-page-transition-enabled,ajax_updown,page_not_loaded,,vertical_menu_enabled,side_area_uncovered_from_content,qode-theme-ver-28.7,qode-theme-bridge,disabled_footer_top,qode_header_in_grid,wpb-js-composer js-comp-ver-6.8.0,vc_responsive,elementor-default,elementor-kit-6

Fix EntityRef: expecting ; PHP error when reading public RSS feeds

Fix EntityRef: expecting ; PHP error when reading public RSS feeds

It is still very common to encounter RSS feeds that have simple XML parsing errors. This usually occurs when the RSS feed is created using a templating system instead of an XML library. The most common error that I’ve seen is a bare ampersand ‘&’ in the title or description. Ampersands are special characters in XML used to denote entities, and ampersands in text must be encoded as the XML entity & for the RSS to be valid.

To fix this, I use the following regular expression:

$rss_string = preg_replace('/&(?!;{6})/', '&',  $rss_string);

This regular expression uses negative lookahead to find any ampersand characters that are NOT followed by a semicolon within six characters. Any ampersands not followed by a semicolon are probably bare ampersands, and they are replaced with the & entity.

Fixing incoming RSS this way is just a workaround. It would be nice if all public RSS feeds were XML compliant, but sadly that’s not the case.

credit: https://www.php.net/manual/en/simplexml.examples-errors.php#115708



Mastodon