Php Can';t使用str_replace删除特殊字符
我对str_replace有一个很小的问题 我有一个带破折号(-)的字符串,如下所示:Php Can';t使用str_replace删除特殊字符,php,str-replace,html-encode,Php,Str Replace,Html Encode,我对str_replace有一个很小的问题 我有一个带破折号(-)的字符串,如下所示: I want to remove - the dash str_replace(html_entity_decode('–', ENT_COMPAT, 'UTF-8'), '', $string); html输出为 I want to remove the – the dash 我想这样做: $new_string = str_replace ('-','',$s
I want to remove - the dash
str_replace(html_entity_decode('–', ENT_COMPAT, 'UTF-8'), '', $string);
html输出为
I want to remove the – the dash
我想这样做:
$new_string = str_replace ('-','',$string);
我尝试用html_entity_decode解析字符串,用htmlspecialchars解析要删除的字符,但没有任何结果
我做错了什么
-编辑-
这是我脚本的完整代码:
$title = 'Super Mario Galaxy 2 - Debut Trailer'; // Fetched from the DB, in the DB the character is - (minus) not –
$new_title = str_replace(' - ', '', $title);
$new_title = str_replace(" - ", '', $title);
$new_title = str_replace(html_entity_decode('–'),'',$title);
没有人工作。
基本上,问题是在DB中,破折号存储为“减号”(我用减号键输入值),但出于奇怪的原因,输出为&ndash
我在Wordpress上运行,字符集是UTF-8,DB排序规则也是如此。试试这个:
$new_string = str_replace('–','',$string);
或:
这基本上与:
$new_string = str_replace ('-','',$string);
有
&ndash代码>(–),还有减号(-)。确保您没有尝试替换错误的字符。尝试以下操作:
I want to remove - the dash
str_replace(html_entity_decode('–', ENT_COMPAT, 'UTF-8'), '', $string);
我猜这不是一个真正的ndash,而是一个非常相似的角色。我建议拉动字符串中每个字符的字节值,看看它是什么样子:
function decodeString($str) {
//Fix for mb overloading strlen option
if (function_exists('mb_strlen')) {
$len = mb_strlen($str, '8bit');
} else {
$len = strlen($str);
}
$ret = '';
for ($i = 0; $i < $len; $i++) {
$ret .= dechex(ord($str[$i])).' ';
}
return trim($ret);
}
函数解码字符串($str){
//修复了mb重载strlen选项
如果(函数_存在('mb_strlen')){
$len=mbstrlen($str,'8bit');
}否则{
$len=strlen($str);
}
$ret='';
对于($i=0;$i<$len;$i++){
$ret.=dechex(ord($str[$i])”;
}
返回修剪($ret);
}
这将把字符串转换为单个字节编码(将其转换为十六进制字符串,如48 65 6C 6C 6F
(Hello
)。检查两种情况下的破折号实际上是同一个字符。如果看到“2D”破折号所在的位置是一个文字减号…如果您看到三字节序列e28093
,那就是&ndash;
。其他任何字符都表示不同的字符
编辑:
如果您看到266e646173683b
这是一个文本
,那么您需要执行stru替换('&ndash;','$str);
这是我对无效ndash的解决方案:
$string = str_replace(chr(hexdec('3f')), '-', $string);
我通过在functions.php中调用remove\u filter('the_title','wpturesize');
来实现这一点,然后您可以执行str\u replace
或以“-”号执行任何操作;我尝试了所有方法,但没有任何效果。但最终在
这个代码确实对我有用
$arr1 = explode(",","0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F");
$arr2 = explode(",","B,C,D,E,F");
foreach($arr2 as $t1){
foreach($arr1 as $t2){
$val = $t1.$t2;
$desc = str_replace(chr(hexdec($val)),"",$desc);
}
}
// if need removing individual value
$desc = str_replace(chr(hexdec('A2')),"",$desc);
只有这个解决方案对我有效:
$string = str_replace("\x96", "-", $string);
对于那些尝试了以上所有方法但仍然没有任何乐趣的人来说,这对我来说很有效(通过WordPress获取标题()
函数)
我已经尝试了所有这些,但没有运气。我真的无法理解。在db中,虚线存储为-,而不是以“&ndash;”形式,但当我输出时,它显示为“&n dash”…我确信这是&ndash,我已经查看了html源代码。如果您指的是文字&ndash;
,则应该应用first sAc的解决方案。是否使用htmlentities()转义输出
?您是在转义之前还是之后进行替换?对这两个问题都没有。这很奇怪。我将用我拥有的确切代码更新OP。我无法用您的示例代码重现此问题。如果是编码问题,您可能需要将其保存到文件中并上载到某个位置……您粘贴的代码可以正常工作。因此,显然您的问题是som其他的…用你的第一个str_替换所有的东西都可以!现在我很好奇为什么我要输出ndash而不是负数…我来看看十六进制字符串。谢谢!那么问题是你的源代码文件实际上不是UTF-8(你认为是ndash的字符是其他字符)…如果您使用UTF-8字符集,请始终将'UTF-8'
参数添加到html(_entity\u decode | entities | specialchars)
您救了我的命!谢谢!我遇到了致命错误:调用未定义函数remove\u filter()