【问题标题】:Some characters in the string cannot be replaced字符串中的某些字符无法替换
【发布时间】:2017-03-22 12:17:57
【问题描述】:

我正在尝试更改此字符串

测试 - GGA ökologie Geschäftsführer.PDF

进入这个:

test__-_gga_oekologie_geschaaeftsfuuehrer.pdf

这是我尝试过的:

$characters = array('ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', ' ');
$converted_characters = array('ae', 'oe', 'ue', 'AE', 'OE', 'UE', '_');
$string = 'Test - GGA ökologie Geschäftsführer.PDF';

echo strtolower(str_replace($characters, $converted_characters , $string));

上面的代码返回:test__-_gga_ökologie_geschäftsführer.pdf

如您所见,字符串仍然包含字符 öä

我的代码有什么问题?

【问题讨论】:

  • 尝试添加 header('Content-Type: text/html; charset=utf-8');
  • 你试过双引号吗?
  • 很可能是编码问题。在我使用 utf8 保存的 php 文件中的代码进行的测试中,一切都像魅力一样。
  • 您的输入字符串似乎是乱码。只需用键盘输入变音符号,您的代码就可以工作了
  • 在 5.5 5.6 7.0 中测试过,您的字符串编码错误。当您手动输入时,它工作正常

标签: php


【解决方案1】:

它们不是同一个 unicode 字符。如果您从字符串中复制并粘贴 字符并将其粘贴到数组中,它将按预期工作。

【讨论】:

    【解决方案2】:

    使用正则表达式

    $characters = array('/ä/u', '/ö/u', '/ü/u', '/Ä/u', '/Ö/u', '/Ü/u', '/ /' );
    $converted_characters = array('ae', 'oe', 'ue', 'AE', 'OE', 'UE', '_');  
    $string = 'Test - GGA ökologie Geschäftsführer.PDF';
    
    echo strtolower(preg_replace($characters, $converted_characters , $string));
    

    【讨论】:

      【解决方案3】:

      试试这个。我已经检查过了。您必须将字符串中的字符复制并替换为数组。它会完成这项工作。数组和字符串中的字符不同。

      【讨论】:

        猜你喜欢
        • 2018-08-23
        • 1970-01-01
        • 2023-03-04
        • 2012-03-30
        • 2011-02-21
        • 2014-12-13
        • 2013-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多