将Æ转换为"Ae".在PHP中使用Str_replace? [英] Converting Æ to "Ae" In PHP With Str_replace?

查看:78
本文介绍了将Æ转换为"Ae".在PHP中使用Str_replace?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于业务逻辑的理由,我需要将字符串中的字符Æ"转换为"Ae".但是,尽管mb_detect_encoding()告诉我该字符串是UTF-8,但我不知道该怎么做. (并且出于其他业务逻辑原因,就像其他Google搜索所建议的那样,在替换字符串之前htmlentities()会成为问题.)

For reasons justified by business logic, I need to convert the character "Æ" to "Ae" in a string. However, despite the fact that mb_detect_encoding() tells me the string is UTF-8, I can't figure out how to do this. (And for other reasons of business logic, it would be an issue to htmlentities() the string before replacing it, as other Google searches have suggested.)

我首先尝试使用测试字符串Æther":

What I tried first was this, using the test string "Æther":

return str_replace("Æ", 'Ae', $string); 

不幸的是,实际上并没有在文本中找到Æ,而是返回了Æther".

Unfortunately, that doesn't actually find the Æ in the text, returning "Æther".

return str_replace(chr(195), 'Ae', $string); 

找到并替换Æ,但之后添加一个未知字符,将其更改为不可用的以太".所以我尝试了这个:

That finds the Æ and replaces it, but adds an unknown character afterwards, changing it to the not-usable "Ae�ther." So I tried this:

$ae_character = mb_convert_encoding('&#' . intval(195) . ';', 'UTF-8', 'HTML-ENTITIES');
return str_replace($ae_character, 'Ae', $string); 

再次未能在字符串中找到Æ字符.我知道这是某种UTF-8问题,但老实说我很困惑如何在不添加额外字符的情况下搜索和替换它.有任何想法吗?

Which again failed to find the Æ character in the string. I know it's a UTF-8 issue of some sort, but I'm honestly stumped as to how to search for and replace this without adding the extra character afterwards. Any ideas?

推荐答案

<?php
$x = 'Æmystr';
print str_replace('Æ', 'AE', $x); // prints: AEmystr
?>

该代码可以正常工作,我认为您缺少的是更改文件的编码.您的.php文件应使用UTF-8或UNICODE编码.可以在某些(文本)编辑器或IDE(例如Eclipse,EditPlus,Notepad ++等)中完成此操作,甚至在Windows 7中也可以使用记事本.

That code works just fine, what I believe you're missing is changing the encoding of your file. Your .php file should be encoded in UTF-8 or UNICODE. This can be done in some (text) editors or IDEs, i.e Eclipse, EditPlus, Notepad++ etc... Even Notepad on windows 7.

保存时,弹出保存/另存为"对话框,通常在保存"按钮附近有一个编码"下拉/单选按钮,可让您在ANSI和UTF-8(及其他)之间进行选择.

When saving bring up the Save/Save As dialog, and normally near the Save button there is an Encoding dropdown/radio buttons, that lets you choose between ANSI and UTF-8 (and others).

在* nix上,我相信大多数编辑器都具有,只是不确定位置.如果执行此操作并使其正常工作,请使用仅执行ANSI的编辑器进行编辑/保存,否则它将被未知的char等覆盖.

On *nix I believe most editors have it, just not sure of the locations. If after you do it and get it working, then edit/save with an editor that just does ANSI it'll overwrite it with an unknown char etc...

下面的代码为什么不起作用.

As to why the below code didn't work.

return str_replace(chr(195), 'Ae', $string); 

这是因为unicode字符通常是2个字符.因此,您所拥有的仅仅是unicode char的开始.试试这个:

It's because a unicode char is normally 2 chars put together. So what you have above is just the start of the unicode char. try this:

print str_replace(chr(195).chr(134), 'AE', $x);

那也应该替换掉它,甚至可能是首选,因为您(可能|不必)不必更改文件编码.

That should replace it as well and might even be preferred as you (might|do) not have to change the file encoding.

这篇关于将Æ转换为"Ae".在PHP中使用Str_replace?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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