str_replace 给出错误的结果
我得到的数据是这样的:“טІТВЮаרй”。 将此数据十六进制化为:d398d086d2a2d292d2aed2b0d29ad3a8d2ba 然后为 *.rtf 格式添加“\'”: \'d3\'8d\'86\'2a\'d2\'2d\'ae\'2b\'d2\'ad\'a8\'2b
然后我必须得到类似这样的东西: \u1179\'3f\u1240\'3f\u1186\'3f...
但 str_replace 仅替换斜杠 Q_Q。
有什么建议吗?
这是完整的代码:
<?
function strToHex($string)
{
$hex='';
for ($i=0; $i < strlen($string); $i++)
{
$hex .= dechex(ord($string[$i]));
}
return $hex;
}
function extra($txt) {
$output_arr = array (
//
"\\u1179\\'3f","\\u1240\\'3f","\\u1186\\'3f","\\u1170\\'3f","\\u1198\\'3f","\\u1200\\'3f","\\u1178\\'3f","\\u1256\\'3f","\\u1210\\'3f"
);
$input_arr = array (
//
"\\'d3\\'98","\\'d0\\'86","\\'d2\\'a2","\\'d2\\'92","\\'d2\\'ae","\\'d2\\'b0","\\'d2\\'9a","\\'d3\\'a8","\\'d2\\'ba"
);
echo "<br>";
echo "data: ".$txt."<br>";
$txt = strtohex($txt);
echo "hex: ".$txt."<br>";
for ($ii=0; $ii < strlen($txt); $ii++) {
//
if (strlen($tm1)<2) {
//
$tm1.=substr($txt,$ii,1);
}
else
{
//
$ret.="\\'".$tm1;
$tm1='';
}
}
echo "RET:[".$ret."]<br>";
$ret = str_replace($input_arr,$output_arr,$ret);
echo "RETREP:[".$ret."]<br>";
return $ret;
}
extra("ӘІҢҒҮҰҚӨҺ");
?>
I got data like this: "ӘІҢҒҮҰҚӨҺ".
hexing this data to this: d398d086d2a2d292d2aed2b0d29ad3a8d2ba
then adding "\'" for *.rtf format: \'d3\'8d\'86\'2a\'d2\'2d\'ae\'2b\'d2\'ad\'a8\'2b
and then I must get somethingl ike this: \u1179\'3f\u1240\'3f\u1186\'3f...
but str_replace replaces only slashes Q_Q.
Any suggestions?
here is full code:
<?
function strToHex($string)
{
$hex='';
for ($i=0; $i < strlen($string); $i++)
{
$hex .= dechex(ord($string[$i]));
}
return $hex;
}
function extra($txt) {
$output_arr = array (
//
"\\u1179\\'3f","\\u1240\\'3f","\\u1186\\'3f","\\u1170\\'3f","\\u1198\\'3f","\\u1200\\'3f","\\u1178\\'3f","\\u1256\\'3f","\\u1210\\'3f"
);
$input_arr = array (
//
"\\'d3\\'98","\\'d0\\'86","\\'d2\\'a2","\\'d2\\'92","\\'d2\\'ae","\\'d2\\'b0","\\'d2\\'9a","\\'d3\\'a8","\\'d2\\'ba"
);
echo "<br>";
echo "data: ".$txt."<br>";
$txt = strtohex($txt);
echo "hex: ".$txt."<br>";
for ($ii=0; $ii < strlen($txt); $ii++) {
//
if (strlen($tm1)<2) {
//
$tm1.=substr($txt,$ii,1);
}
else
{
//
$ret.="\\'".$tm1;
$tm1='';
}
}
echo "RET:[".$ret."]<br>";
$ret = str_replace($input_arr,$output_arr,$ret);
echo "RETREP:[".$ret."]<br>";
return $ret;
}
extra("ӘІҢҒҮҰҚӨҺ");
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的代码没有直接问题,除了您用作示例的字符串不包含
$input_arr
中的任何序列这一事实之外。我手动将\'d3\'8d
添加到该列表中,并且替换工作正常,因此这可能是问题的根源。您似乎正在将 UTF-8 转换为 ASCII 表示形式,将 Unicode 字符转义为
\u{code}\'3f
,因此您也许可以利用 此评论中描述的utf8tohtml
函数,它转义&#{code};
格式。I see no immediate problem with your code, other than the fact that the string you use as an example contains none of the sequences in
$input_arr
. I added manually\'d3\'8d
to that list, and the replacement worked correctly, so this might be the source of your problem.You appear to be converting an UTF-8 to an ASCII representation that escapes Unicode characters as
\u{code}\'3f
, so you might be able to leverage theutf8tohtml
function described in this comment, which escapes characters in the&#{code};
format.由于“for”循环中的“if”逻辑,我得到了错误的结果。
这是正确的:
在旧版本(问题)中,这个东西会跳过主字符串的每三个字符。所以现在可以正常工作了。
I was getting wrong results because of "if" logic in "for" loop.
Here is the right one:
In old version (question) this thing was skipping every third char of the main string. So now it works OK.