简体   繁体   English

可以在 php 字符串中替换 ¼ 吗?

[英]Replacing ¼ in a php string possible?

我如何在 php 字符串中替换¼我刚刚尝试了以下代码但它不起作用

 $directions=str_replace("¼", "1/4", $directions);

You seem to be suffering as a result of PHP's inability to gracefully handle Unicode.您似乎因为 PHP 无法优雅地处理 Unicode 而受苦。

The character in question can be represented in some versions of extended ASCII, and not in others.有问题的字符可以用某些版本的扩展 ASCII 表示,而不能用其他版本表示。 It can also be represented in Unicode, by a multi-byte sequence.它也可以用 Unicode 表示,由多字节序列表示。

What works depends on the input (HTTP input), script (php file), and any other sources (database etc) encoding being consistent.什么工作取决于输入(HTTP 输入)、脚本(php 文件)和任何其他来源(数据库等)编码是否一致。

This character happens to be representable in both ASCII and Unicode, but it seems like you are working on an application that does need to be Unicode.这个字符恰好可以用 ASCII 和 Unicode 表示,但看起来您正在开发一个确实需要是 Unicode 的应用程序。

Once your encoding problem is solved, you can use multi-byte string functions:一旦你的编码问题解决了,你就可以使用多字节字符串函数:

echo mb_ereg_replace("¼", "1/4", "¼ of things");

这对我有用

$directions=str_replace(chr(188), "1/4", $directions);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM