15 Mar 2023 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