0

我正在使用 preg_match_all 过滤掉字符串

我在 preg_match_all 中提供的字符串是

$text = "Friedric'h Wöhler"

之后我使用

preg_match_all('/(\"[^"]+\"|[\\p{L}\\p{N}\\*\\-\\.\\?]+)/', $text, $arr, PREG_PATTERN_ORDER);

当我打印 $arr 时得到的结果是

Array
(
    [0] => Array
        (
            [0] => friedric
            [1] => h
            [2] => w
            [3] => ouml
            [4] => hler
        )

    [1] => Array
        (
            [0] => friedric
            [1] => h
            [2] => w
            [3] => ouml
            [4] => hler
        )

)

不知何故, ö 字符被 ouml 取代,我不确定如何解决这个问题

我期待以下结果

Array
(
    [0] => Array
        (
            [0] => Friedric'h 
            [1] => Wöhler
        )

)
4

2 回答 2

1

根据 nhahtdh 的评论:

$text = "Friedric'h Wöhler";
preg_match_all('/"[^"]+"|[\p{L}\p{N}*.?\\\'-]+/u', $text, $arr, PREG_PATTERN_ORDER);
echo "<pre>";
print_r($arr);
echo "</pre>";

Array
(
    [0] => Array
        (
            [0] => Friedric'h
            [1] => Wöhler
        )

)
于 2013-02-16T06:24:27.723 回答
0

如果你觉得preg_match_all()很乱,你可以看看pattern()

$p = '"[^"]+"|[\p{L}\p{N}*.?\\\'-]+';  // automatic delimiters
$text = "Friedric'h Wöhler";

$result = pattern($p)->match($text)->all();
于 2019-01-24T16:03:54.947 回答