无法使用 str_replace 删除特殊字符

发布于 2024-09-07 15:38:15 字数 858 浏览 8 评论 0原文

我对 str_replace 有一个非常微不足道的问题。

我有一个带有短划线字符( - )的字符串,如下所示:

I want to remove - the dash

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);

没有人工作。 基本上问题是,在数据库中,破折号存储为“减号”(我用减号键输入值),但出于一个奇怪的原因,输出是 &ndash ;

我在 Wordpress 上运行,字符集是 UTF-8,数据库排序规则也是如此。

I have a very trivial problem with str_replace.

I have a string with the En Dash character ( - ) like this:

I want to remove - the dash

The html output is

I want to remove the – the dash

I want to do this:

$new_string = str_replace ('-','',$string);

I've tried to parse the string with html_entity_decode, to parse the character to remove with htmlspecialchars,but without any results.

What I'm doing wrong?

-EDIT-
This is the full code of my script:

$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);

No one works.
Basically the problem is that in the DB the dashes are stored as "minus" (I enter the value with the minus key) but for a strange reason the output is &ndash ;

I'm running on Wordpress and the charset is UTF-8, the same for the DB collation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

Hello爱情风 2024-09-14 15:38:16

尝试这样的事情:

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);
}

这会将字符串转换为单独的字节编码(将其转换为十六进制字符串,如 48 65 6C 6C 6F (Hello)。检查一下这两种情况下的破折号实际上是同一个字符。如果您看到破折号所在的位置是“2D”,那么它就是一个减号...如果您看到。三字节序列 E2 80 93,即 。其他任何内容都表示不同的字符...

编辑:
如果您看到 26 6E 64 61 73 68 3B 是一个文字 ,那么您需要执行 str_replace('&ndash ;', '', $str);

try something like this:

str_replace(html_entity_decode('–', ENT_COMPAT, 'UTF-8'), '', $string);

My guess is it's not really an ndash, but a very similar character. I'd suggest pulling the byte values of each character in the string to see what it looks like:

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);
}

That'll convert the string into the individual byte encodings (turn it into a hex string like 48 65 6C 6C 6F (Hello). Check to see the dash in both cases is in fact the same character. If you see "2D" where the dash is, that's a literal minus sign... If you see the three byte sequence E2 80 93, that's . Anything else means a different character...

EDIT:
And if you see 26 6E 64 61 73 68 3B that mens a literal , so you'd need to do str_replace('–', '', $str);

七七 2024-09-14 15:38:16

我已经成功地通过在functions.php中调用remove_filter('the_title','wptexturize');来做到这一点,然后你执行str_replace或通过“-”执行任何操作符号;

i've managed to do this by calling remove_filter( 'the_title', 'wptexturize' ); in functions.php an then you perform a str_replace or whatever by "-" sign;

公布 2024-09-14 15:38:16

(–) 和减号 (-)。确保您没有尝试替换错误的字符。

There's (–) and there's the minus sign (-). Make sure you are not trying to replace the wrong character.

十秒萌定你 2024-09-14 15:38:16

我尝试了一切,但没有任何效果。但最终在 http://www.ascii.cl/htmlcodes.htm 的帮助下

这段代码对我有用

        $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);

I tried everything and nothing worked. but in the end with the help of http://www.ascii.cl/htmlcodes.htm

this code did work for me

        $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);
攒眉千度 2024-09-14 15:38:16

试试这个:

$new_string = str_replace('–','',$string);

或者:

$new_string = str_replace(html_entity_decode('–'),'',$string);

它基本上与以下相同:

$new_string = str_replace ('-','',$string);

Try this:

$new_string = str_replace('–','',$string);

Or:

$new_string = str_replace(html_entity_decode('–'),'',$string);

It is basically same as:

$new_string = str_replace ('-','',$string);
小霸王臭丫头 2024-09-14 15:38:16

这是我对无效 ndash 的解决方案:

$string = str_replace(chr(hexdec('3f')), '-', $string);

This was my solution for an invalid ndash:

$string = str_replace(chr(hexdec('3f')), '-', $string);
我也只是我 2024-09-14 15:38:16

只有这个解决方案对我有用:

$string = str_replace("\x96", "-", $string);

Only this solution worked for me:

$string = str_replace("\x96", "-", $string);
︶ ̄淡然 2024-09-14 15:38:16

对于尝试过上述所有方法但仍然没有乐趣的人来说,这对我有用(来自 WordPress get_the_title() 函数)

$new_string = str_replace('–', 'or', $string);

To anyone who has tried all of the above but still having no joy then this worked for me (from a WordPress get_the_title() function)

$new_string = str_replace('–', 'or', $string);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文