PHP preg_match 将花括号与其他类型的花括号不匹配.如何避免? [英] PHP preg_match is mismatching a curly apostrophe with other types of curly quotes. How to avoid?

查看:42
本文介绍了PHP preg_match 将花括号与其他类型的花括号不匹配.如何避免?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下变量内容:

$content_content = '我做不到,她说."';

我想对其中的每个单词"进行 preg_match,包括缩写,所以我使用 preg_match 如下:

 if (preg_match_all('/([a-zA-Z0-9']+)/', $content_content, $matches)){echo '

';print_r($matches);echo '</pre>';}

然而,似乎通过在正则表达式中包含 ’,它也捕获了双引号,如上面的命令输出:

数组([0] =>大批([0] =>[1] =>一世[2] =>不能[3] =>做[4] =>它[5] =>她[6] =>说[7] =>)[1] =>大批([0] =>[1] =>一世[2] =>不能[3] =>做[4] =>它[5] =>她[6] =>说[7] =>))

如何在不包含和"的情况下包含 ’?

解决方案

这是因为您在字符集中使用的花哨"撇号是以二进制形式处理的;您需要使用其各自的 modifier 启用 Unicode 模式:

preg_match_all('/([a-zA-Z0-9']+)/u', $content_content, $matches)

演示

I have the following variable content:

$content_content = '"I can’t do it, she said."';

I want to do a preg_match for every "word" in that, including the contractions, so I use preg_match as follows:

 if (preg_match_all('/([a-zA-Z0-9’]+)/', $content_content, $matches))
 {
    echo '<pre>';
    print_r($matches);
    echo '</pre>';
 }

However, it seems by including ’ in the regular expression, it's also trapping the curly double quotes, as the above command outputs:

Array
(
    [0] => Array
        (
            [0] => ��
            [1] => I
            [2] => can’t
            [3] => do
            [4] => it
            [5] => she
            [6] => said
            [7] => ��
        )

    [1] => Array
        (
            [0] => ��
            [1] => I
            [2] => can’t
            [3] => do
            [4] => it
            [5] => she
            [6] => said
            [7] => ��
        )

)

How can I include ’ without it also including the " and "?

解决方案

This is because the "fancy" apostrophe you're using inside the character set is treated in its binary form; you need to enable Unicode mode using its respective modifier:

preg_match_all('/([a-zA-Z0-9’]+)/u', $content_content, $matches)

Demo

这篇关于PHP preg_match 将花括号与其他类型的花括号不匹配.如何避免?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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