【问题标题】:Remove particular UTF-8 character from string with PHP使用 PHP 从字符串中删除特定的 UTF-8 字符
【发布时间】:2015-03-12 13:08:46
【问题描述】:

我从远程 API 获得了一些字符串,有人包含 char ð(我无法摆脱或替换它)

我试过了

$str = str_replace("ð", "", $str); 

$str = strtr($str,'ð','');

没有效果

【问题讨论】:

  • 您的 php 文件是否编码为 utf-8?并担任 utf-8?

标签: php replace


【解决方案1】:

根据我的说法,您的 php 文件未以 UTF-8 编码。使用标题指定它。使用下面的代码

<?php
header('Content-Type: text/html; charset=utf-8');
$str="hð";
$str = strtr($str,'ð','');
echo $str; // print h

    <?php
    header('Content-Type: text/html; charset=utf-8');
        $str="hð";
    $str = str_replace("ð", "", $str); 
echo $str; // prints h

希望对你有帮助

【讨论】:

    【解决方案2】:

    尝试替换它的 html 实体,例如:

    str_replace("&eth;","",$str);
    

    【讨论】:

      【解决方案3】:
      $str = iconv( "UTF-8", "ISO-8859-1//TRANSLIT", $str);
      or
      $str = iconv( "UTF-8", "ISO-8859-1//TRANSLIT//IGNORE", $str);
      

      希望这会奏效。 使用 iconv() 代替 str_replace

      【讨论】:

        猜你喜欢
        • 2014-02-12
        • 1970-01-01
        • 2013-12-22
        • 2018-01-05
        • 2018-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-19
        相关资源
        最近更新 更多