为什么会有�在我的页面上
我已经设置了页面默认字符集和 MySQL 表字符集 utf8。在某些页面上运行良好,但在某些页面上输出某些汉字如“全”、“公”时显示为�,而在其他页面上却可以正常输出。
唯一的区别通常页面和错误页面我意识到我在错误页面上输出之前使用了一些 ereg_replace 。
$sounds = nl2br($model->sounds);
$sounds= preg_replace('/(\v|\s)+/', ' ', $sounds);
$sounds= preg_replace("#(<br />|<br /> )+[< b r > \ ]*[<br />| <br /> ]+#","<br>",$sounds);
$pattern='#[\d]+[\-]*[\d]*[\.]+#';
if(preg_match($pattern,$sounds)&&!preg_match('#<br />|<br />|<br>#',$sounds))
{
$sounds= preg_replace("#[\d]+[\-]*[\d]*[\.]+#","<br>",$sounds);
}
这些功能可能是原因吗?或者还有什么原因呢?
更新:
我在评论 $sounds= preg_replace('/(\v|\s)+/', ' ', $sounds);
时发现工作正常,但我想使用这一行删除数据中的多个空格。有什么替代方法可以做到这一点?
I've already set my page default charset and MySQL table charset utf8. It works well on some of the pages, but on some pages when output some certain Chinese characters like '全' and '公' it appears to be � while on other pages they can be output normally.
The only difference between the normally pages and the error pages i realize is I used some ereg_replace before output on the error page.
$sounds = nl2br($model->sounds);
$sounds= preg_replace('/(\v|\s)+/', ' ', $sounds);
$sounds= preg_replace("#(<br />|<br /> )+[< b r > \ ]*[<br />| <br /> ]+#","<br>",$sounds);
$pattern='#[\d]+[\-]*[\d]*[\.]+#';
if(preg_match($pattern,$sounds)&&!preg_match('#<br />|<br />|<br>#',$sounds))
{
$sounds= preg_replace("#[\d]+[\-]*[\d]*[\.]+#","<br>",$sounds);
}
Could these functions be the reason? Or what else could the reason be?
Update:
I found when I comment $sounds= preg_replace('/(\v|\s)+/', ' ', $sounds);
it works fine, but i want to use this line to delete multiple white spaces in my data. What's the alternative way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很可能就是原因。使用
u
(UTF-8)修饰符,否则正则表达式可能只匹配某些 Unicode 字符的一部分。另外,我注意到您提到了
ereg_*
但正在使用preg_*
。这很好,总是更喜欢使用preg_*
而不是旧的、缓慢的和已弃用的ereg_*
函数。That could very well be the reason. Use the
u
(UTF-8) modifier, otherwise the regular expression is likely to match only parts of some Unicode characters.Also, I noticed you mentioned
ereg_*
but are usingpreg_*
. That's good, always prefer usingpreg_*
instead of the old, slow and deprecatedereg_*
functions.您必须在模式后面添加 u 修饰符,如下所示:
正如您在此处看到的:
http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
You have to add the u modifier after your pattern like this:
as you can see here:
http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
您可能应该使用 mb_ereg_replace 而不是 ereg_replace。
You should probably use mb_ereg_replace instead of ereg_replace.