PHP:特殊字符成为子字符串中的问号图标 [英] PHP: Special char becomes question mark icon in substring

查看:48
本文介绍了PHP:特殊字符成为子字符串中的问号图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的子字符串函数有问题.当一个特殊字符是结果子字符串的最后一个字符时(并且只有那个!!!),这个字符被表示为一个带有问号的图标.

I have a problem with a substring function. When a special char is the last char of the resulting substring (and only then!!!), this chars gets represented as an icon with a question mark in it.

这是我的代码:

$string = 'This is a string and when a German word with a special char like "Tür" appears and the special char ü is the last character of the substring it gets represented as an icon with question mark';
echo substr($string,0,102). "...";

结果:

这是一个字符串,当德语单词带有像Tür"这样的特殊字符时出现和特殊字符 ...

This is a string and when a German word with a special char like "Tür" appears and the special char �...

我怎样才能避免这种情况?

How can I avoid this?

推荐答案

听起来您正在使用 8 位字符串函数来处理 unicode 字符.那行不通.

Sounds like you are using 8bit string functions to process unicode characters. That cannot work.

您应该安装 mbstring 包并在您的 php 配置中启用mbstring 函数重载".这将注意以静默方式覆盖所有相关的字符串处理函数以使用它们的多字节安全等价物,这样您就不必更改代码.

You should install the mbstring package and enable "mbstring function overloading" in your php configuration. That will take care to silently override all relevant string handling functions to use their multi byte safe equivalents instead, so that you do not have to change your code.

该mbstring"扩展有许多配置选项.检查您的 php.ini 配置文件并查看它们.您将找到mbstring.func_overload 命令,您可能希望将其设置为值7,以便覆盖所有 此类函数.更改该配置后,您必须重新启动 http 服务器进程,以便加载新配置.您还可以通过在测试脚本中使用著名的 phpinfo() 函数来检查这一点.

There are a number of configuration options for that "mbstring" extension. Check your php.ini configuration file and look through them. You will find the mbstring.func_overload command which you probably want to set to the value 7, so that all such functions are overridden. After changing that configuration you have to restart your http server process, so that the new configuration gets loaded. You can also check that by using the famous phpinfo() function in a test script.

另一种方法是不配置此覆盖​​自动魔术,而是手动移植您的代码以直接使用这些功能.为此,您必须将代码中的 all 字符串函数调用替换为它们的等效函数名称.例如 mb_substr(...) 而不是 substr(...).

An alternative would be not to configure this overriding automagic, but to manually port your code to directly use those functions. For that you will have to replace all string function calls in your code by their equivalent function names. So for example mb_substr(...) instead of just substr(...).

此外,您真的想开始阅读您使用的工具的文档.这里对那些多字节字符串"函数的介绍很有趣.它应该可以帮助您了解这一切是关于什么以及您必须注意什么:http://php.net/manual/en/book.mbstring.php

Also you really want to start reading the documentation of the tools you use. Here the introduction to those "multi byte string" functions is of interest. It should help you to understand what this all is about and what you have to take care of: http://php.net/manual/en/book.mbstring.php

这篇关于PHP:特殊字符成为子字符串中的问号图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆