protected function escapeXliff($content)
{
$count = 1;
$openTags = [];
$final = [];
// remove nasty device control characters
$content = preg_replace('/[\\x00-\\x09\\x0B\\x0C\\x0E-\\x1F\\x7F]/', '', $content);
$replacement = ['%_%_%lt;%_%_%', '%_%_%gt;%_%_%'];
$content = str_replace(['<', '>'], $replacement, $content);
$content = html_entity_decode($content, null, "UTF-8");
if (!preg_match_all("/<([^>]+)>([^<]+)?/", $content, $matches)) {
// return original content if it doesn't contain HTML tags
return '<![CDATA[' . $content . ']]>';
}
foreach ($matches[0] as $match) {
$parts = explode(">", $match);
$parts[0] .= ">";
foreach ($parts as $part) {
$part = trim($part);
if (!empty($part)) {
if (preg_match("/<([a-z0-9\\/]+)/", $part, $tag)) {
$tagName = str_replace("/", "", $tag[1]);
if (strpos($tag[1], "/") === false) {
$openTags[$count] = ["tag" => $tagName, "id" => $count];
$part = '<bpt id="' . $count . '"><![CDATA[' . $part . ']]></bpt>';
$count++;
} else {
$closingTag = array_pop($openTags);
$part = '<ept id="' . $closingTag["id"] . '"><![CDATA[' . $part . ']]></ept>';
}
} else {
$part = str_replace($replacement, ['<', '>'], $part);
$part = '<![CDATA[' . $part . ']]>';
}
if (!empty($part)) {
$final[] = $part;
}
}
}
}
$content = implode("", $final);
return $content;
}