gpt4 book ai didi

php - 将 JavaScript 函数转换为 PHP 函数(用于将字符串转换为 HTML 编码文本)

转载 作者:行者123 更新时间:2023-11-28 21:13:27 27 4
gpt4 key购买 nike

根据这里的函数,http://www.unicodetools.com/unicode/convert-to-html.php ,该函数用于将字符串转换为HTML编码文本。

JavaScript 是:

function a(b) {
var c= '';

for(i=0; i<b.length; i++) {
if(b.charCodeAt(i)>127) {
c += '&#' + b.charCodeAt(i) + ';';
} else {
c += b.charAt(i);
}
}

document.forms.conversionForm.outputText.value = c;
}

我的尝试是:

function str_to_html_entity($str) {
$output = NULL;

for($i = 0; $i < strlen($str); $i++) {
if(ord($str) > 127) {
$output .= '&#' + ord($str) + ';';
} else {
$output .= substr($str, $i);
}
}

return $output;
}

echo str_to_html_entity("Thére Àre sôme spëcial charâcters ïn thìs têxt");

我的 PHP 函数运行正确,但结果不是我所期望的:

我的结果:

Thére Àre sôme spëcial charâcters ïn thìs têxthére Àre sôme spëcial charâcters ïn thìs têxtére Àre sôme spëcial charâcters ïn thìs têxt�re Àre sôme spëcial charâcters ïn thìs têxtre Àre sôme spëcial charâcters ïn thìs têxte Àre sôme spëcial charâcters ïn thìs têxt Àre sôme spëcial charâcters ïn thìs têxtÀre sôme spëcial charâcters ïn thìs têxt�re sôme spëcial charâcters ïn thìs têxtre sôme spëcial charâcters ïn thìs têxte sôme spëcial charâcters ïn thìs têxt sôme spëcial charâcters ïn thìs têxtsôme spëcial charâcters ïn thìs têxtôme spëcial charâcters ïn thìs têxt�me spëcial charâcters ïn thìs têxtme spëcial charâcters ïn thìs têxte spëcial charâcters ïn thìs têxt spëcial charâcters ïn thìs têxtspëcial charâcters ïn thìs têxtpëcial charâcters ïn thìs têxtëcial charâcters ïn thìs têxt�cial charâcters ïn thìs têxtcial charâcters ïn thìs têxtial charâcters ïn thìs têxtal charâcters ïn thìs têxtl charâcters ïn thìs têxt charâcters ïn thìs têxtcharâcters ïn thìs têxtharâcters ïn thìs têxtarâcters ïn thìs têxtrâcters ïn thìs têxtâcters ïn thìs têxt�cters ïn thìs têxtcters ïn thìs têxtters ïn thìs têxters ïn thìs têxtrs ïn thìs têxts ïn thìs têxt ïn thìs têxtïn thìs têxt�n thìs têxtn thìs têxt thìs têxtthìs têxthìs têxtìs têxt�s têxts têxt têxttêxtêxt�xtxtt

预期结果:

Th&#233;re &#192;re s&#244;me sp&#235;cial char&#226;cters &#239;n th&#236;s t&#234;xt

有人可以告诉我我的 PHP 函数出了什么问题吗?

谢谢

更新

function str_to_html_entity($str) {
$result = null;
for ($i = 0, $length = mb_strlen($str, 'UTF-8'); $i < $length; $i++) {
$character = mb_substr($str, $i, 1, 'UTF-8');
if (strlen($character) > 1) { // the character consists of more than 1 byte
$character = htmlentities($character, ENT_COMPAT, 'UTF-8');
}
$result .= $character;
}

return $result;
}

echo str_to_html_entity("Thére Àre"); // Th&eacute;re &Agrave;re
echo str_to_html_entity("中"); // 中

最佳答案

一般情况下:

因此,您无法在 PHP 中复制完全相同的算法。另外,在循环中,您使用整个 $str 而不是字符串偏移量,这是您的另一个主要问题。为了使其能够识别 Unicode,这可能是最好的方法:

$result = null;
foreach (preg_split('/./u', $str) as $character) {
if (strlen($character) > 1) { // the character consists of more than 1 byte
$character = mb_convert_encoding($character, 'HTML-ENTITIES', 'UTF-8');
}
$result .= $character;
}

这要求字符串采用 UTF-8 编码。正如您所看到的,有一个很好的函数,名为 mb_convert_encoding ,它可以一次性转义一整 block 文本,这实际上是在重新发明。请使用它。

Unicode 受损 PCRE 的替代版本:

$result = null;
for ($i = 0, $length = mb_strlen($str, 'UTF-8'); $i < $length; $i++) {
$character = mb_substr($str, $i, 1, 'UTF-8');
if (strlen($character) > 1) { // the character consists of more than 1 byte
$character = mb_convert_encoding($character, 'HTML-ENTITIES', 'UTF-8');
}
$result .= $character;
}

但说真的,只需使用 $str = mb_convert_encoding($str, 'HTML-ENTITIES', 'UTF-8') 即可完成。无需循环。

关于php - 将 JavaScript 函数转换为 PHP 函数(用于将字符串转换为 HTML 编码文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8162085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com