使用 SOH 控制字符包装 html 特殊字符


wrap html special chars with SOH control character

我从discuz论坛脚本中看到了一个php函数。它将html特殊字符包装<"> &控制字符SOH ,即。CHR(1).如下所示:

<?php
$pre = chr(1);
$end = chr(1);
$string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), $string);
?>

这一行动的目的和功能是什么?完整功能如下:

<?php
function cutstr($string, $length, $dot = ' ...') {
    if(strlen($string) <= $length) {
        return $string;
    }
    $pre = chr(1);
    $end = chr(1);
    $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), $string);
    $strcut = '';
    if(strtolower(CHARSET) == 'utf-8') {
        $n = $tn = $noc = 0;
        while($n < strlen($string)) {
            $t = ord($string[$n]);
            if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                $tn = 1; $n++; $noc++;
            } elseif(194 <= $t && $t <= 223) {
                $tn = 2; $n += 2; $noc += 2;
            } elseif(224 <= $t && $t <= 239) {
                $tn = 3; $n += 3; $noc += 2;
            } elseif(240 <= $t && $t <= 247) {
                $tn = 4; $n += 4; $noc += 2;
            } elseif(248 <= $t && $t <= 251) {
                $tn = 5; $n += 5; $noc += 2;
            } elseif($t == 252 || $t == 253) {
                $tn = 6; $n += 6; $noc += 2;
            } else {
                $n++;
            }
            if($noc >= $length) {
                break;
            }
        }
        if($noc > $length) {
            $n -= $tn;
        }
        $strcut = substr($string, 0, $n);
    } else {
        $_length = $length - 1;
        for($i = 0; $i < $length; $i++) {
            if(ord($string[$i]) <= 127) {
                $strcut .= $string[$i];
            } else if($i < $_length) {
                $strcut .= $string[$i].$string[++$i];
            }
        }
    }
    $strcut = str_replace(array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
    $pos = strrpos($strcut, chr(1));
    if($pos !== false) {
        $strcut = substr($strcut,0,$pos);
    }
    return $strcut.$dot;
}
?>

提前欣赏。

代码试图确定字符串中有多少打印字符,因此它可以将其缩写为一定长度。所以一开始它想用它们的单字符等价物替换所有的 HTML 实体,例如 &amp;替换为 & 。最后,它想把它们放回原来。

但是,如果它只是用&替换&amp;,最后用&amp;替换&,如果原始字符串包含任何不属于&amp;(或第一个替换处理的其他实体之一)的&,它将做错误的事情。因此,它用 SOH 包装所有替换(确定大小的循环忽略控制字符)。然后在最后,它只用它们的实体替换包装的字符,所以这只会撤消原始替换。