具有UTF-8的PHP preg_replace无法正常工作 [英] PHP preg_replace with UTF-8 not working

查看:129
本文介绍了具有UTF-8的PHP preg_replace无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么此preg_replace无法正常工作?

Why is this preg_replace not working?

仅供参考,我将PHP脚本设置为UTF8,而没有BOM,并且我在此处设置的功能是删除模式的所有匹配项(而不是我实际要做的,即删除所有不匹配项),因为这样更容易供测试用.另请注意,字符不在我的正则表达式中,因此这应该是唯一的字符.

FYI, I have the PHP script set to UTF8 Without BOM and I have the function here set to remove all matches of the pattern (instead of what I will actually do, which is remove all non-matches) because that is easier for testing. Note also that the character is not in my regex, so this should be the only character left behind.

$string='The Story of Jewād';
echo preg_replace('@([!"#$&’\(\)\*\+,\-\./0123456789:;<=>\?ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]\^_‘abcdefghijklmnopqrstuvwxyz\{\|\}~¡¢£⁄¥ƒ§¤"«‹›fifl–†‡·¶•‚„"»…‰¿`´ˆ˜¯˘˙¨˚¸˝˛ˇ—ƪŁØŒºæıłøœß÷¾¼¹×®Þ¦Ð½−çð±Çþ©¬²³™°µ ÁÂÄÀÅÃÉÊËÈÍÎÏÌÑÓÔÖÒÕŠÚÛÜÙÝŸŽáâäàåãéêëèíîïìñóôöòõšúûüùýÿž€\'])@u','',$string);

我得到的结果是$ string不变.为什么会这样?

The result I get is $string unchanged. Why would this be?

推荐答案

此方法相反:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<?php 

$string='The Story of Jewād';
echo preg_replace('@([ā])@','',$string);

?>

因此,某处仅存在语法问题... 将所有字符都列为RegExp不是一个好主意.您可以列出类似这样的内容:

So, there is just a syntax problem somewhere ... This isn't a good idea to list all characters as a RegExp. You can do listings something like this:

ltrChars : 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF'+'\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF';
rtlChars : '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC';

这篇关于具有UTF-8的PHP preg_replace无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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