ActiveResource\ActiveResource::_xml_entities PHP Метод

_xml_entities() публичный метод

Makes the specified string XML-safe
См. также: http://www.w3.org/TR/REC-xml/#sec-predefined-ent
Автор: Dom Hastings
public _xml_entities ( string $s, boolean $hex = true ) : string
$s string
$hex boolean Whether or not to make hexadecimal entities (as opposed to decimal)
Результат string The XML-safe result
    public function _xml_entities($s, $hex = true)
    {
        // if the string is empty
        if (empty($s)) {
            // just return it
            return $s;
        }
        $s = (string) $s;
        // create the return string
        $r = '';
        // get the length
        $l = strlen($s);
        // iterate the string
        for ($i = 0; $i < $l; $i++) {
            // get the value of the character
            $o = $this->_unicode_ord($s, $i);
            // valid characters
            $v = $o >= 9 && $o <= 13 || $o == 32 || $o == 33 || $o >= 35 && $o <= 37 || $o >= 40 && $o <= 47 || $o >= 48 && $o <= 57 || $o == 58 || $o == 59 || $o == 61 || $o == 63 || $o == 64 || $o >= 65 && $o <= 90 || $o >= 91 && $o <= 96 || $o >= 97 && $o <= 122 || $o >= 123 && $o <= 126;
            // if it's valid, just keep it
            if ($v) {
                $r .= $s[$i];
                // &
            } elseif ($o == 38) {
                $r .= '&amp;';
                // <
            } elseif ($o == 60) {
                $r .= '&lt;';
                // >
            } elseif ($o == 62) {
                $r .= '&gt;';
                // '
            } elseif ($o == 39) {
                $r .= '&apos;';
                // "
            } elseif ($o == 34) {
                $r .= '&quot;';
                // unknown, add it as a reference
            } elseif ($o > 0) {
                if ($hex) {
                    $r .= '&#x' . strtoupper(dechex($o)) . ';';
                } else {
                    $r .= '&#' . $o . ';';
                }
            }
        }
        return $r;
    }