【问题标题】:match special character using preg_match使用 preg_match 匹配特殊字符
【发布时间】:2014-03-26 23:55:58
【问题描述】:

如果我用这个就可以了:

$string = 'prueba Numero de Documento: 0206728108 Gracias';

preg_match('#Numero de Documento: (\w+)#', $string, $match);
echo $match[1];

输出:0206728108

这不起作用:

$string = 'prueba Número de Documento: 0206728108 Gracias';

preg_match('#Número de Documento: (\w+)#', $string, $match);
echo $match[1];

空输出

【问题讨论】:

  • 不适合我...你能解释一下为什么当我使用它时它会起作用:$string = mb_convert_encoding(html_entity_decode($string), "UTF-8","ISO-8859-1");
  • 可能是因为在 var/string 中你得到了(可能来自表单的值?)你有这样的东西:$string = 'prueba Número de Documento: 0206728108 Gracias';,在 php 代码中:preg_match( '#Número de Documento: (\w+)#', $string, $match)?writecodeonline.com/php 在线测试代码,并检查规范。字符被转换...
  • 我认为html_entity_decode 在那里并不重要。看看添加u 开关是否有帮助。参考:stackoverflow.com/questions/11649019/…

标签: php preg-match


【解决方案1】:

对 unicode 使用 u 修饰符:

preg_match('#Número de Documento: (\w+)#u', $string, $match);
//                              here ___^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多