【问题标题】:PHP output specialchars as hexPHP 将特殊字符输出为十六进制
【发布时间】:2018-07-22 11:37:40
【问题描述】:

有没有办法将特殊字符(例如&符号&)输出为& 而不是&

使用 htmlenties 或 htmlspecialchars 时,我找不到提供此行为的设置。

任何建议。

【问题讨论】:

  • bin2hex('&') 至少会给你 26 部分,不确定这是否是一种调查途径。您要解决的问题是什么?

标签: php hex special-characters


【解决方案1】:

你需要urlencode 然后str_replace 来做到这一点

$string = 'Stackoverflow # & " ';  
$string = urlencode($string);    
$find = ['%', '+']; 
$replace = ['&#x', ' '];
$string = str_replace($find,$replace, $string);
echo $string;

【讨论】:

  • 我尝试使用不同的字符串 'Überschrift Lörüm Ipsûm' 并且 Ü 被破坏。输出是“rschrift Lörüm Ipsûm
【解决方案2】:

我自己想通了。 我使用 mb_ord 与 ord 类似,但它用于 UTF-8 等多字节编码。 此外,它仅在安装 PHP 7.2 时才有效。

<?php 
$text = html_entity_decode('&infin;&auml;&euro;&auml;&atilde;&acirc;',ENT_COMPAT | ENT_HTML401,'UTF-8');
$text_converted = '';
for($i=0;$i<mb_strlen($text,'UTF-8');$i++){
    $text_converted .= sprintf('&#x%X;',mb_ord(mb_substr($text,$i,1,'UTF-8')));
}
echo $text_converted;
echo '<br>';
echo str_replace('&','&amp;',$text_converted);
echo '<br>';
echo $text;
?>

【讨论】:

    猜你喜欢
    • 2012-07-08
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2013-04-03
    • 1970-01-01
    • 2018-01-31
    相关资源
    最近更新 更多