Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XML control-character stripping #281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Parsers/XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@

class XML
{
/**
* Matches xml-encoded character references (decimal or hex) for ASCII control characters group.
* Will match any string that begins with &# and ends with ; and in between is either
* (0?[0-9]|[12][0-9]|3[01]) - a decimal integer between 00 and 31 (leading 0 optional)
* ([xX](0?|1)[0-9A-Fa-f]) - a hex integer between 00 and 1F (upper or lower case, leading 0 optional)
*
* @link https://regexr.com/5c41j
* @link https://en.wikipedia.org/wiki/ASCII#Control_characters
* @link https://www.liquid-technologies.com/XML/CharRefs.aspx
*/
const REGEX_ASCII_CONTROL_CHARACTERS = '/(&#((0?[0-9]|[12][0-9]|3[01])|([xX](0?|1)[0-9A-Fa-f]));)/';

public function parse($string)
{
if ($string instanceof ResponseInterface or $string instanceof Response) {
$string = $string->getBody()->__toString();
}

return new \SimpleXMLElement((string) $string);
$string = (string)$string;

$string = preg_replace(self::REGEX_ASCII_CONTROL_CHARACTERS, '', $string);

return new \SimpleXMLElement($string);
}
}