在PHP中从json字符串中获取西里尔字?

时间:2016-05-04 04:15:51

标签: php regex utf-8

在PHP中从JSON字符串中获取西里尔字? JSON字符串是非结构化的任何类型。

示例:

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Вилен", "lastName":"Авангард"},
    {"firstName":"Станислав", "lastName":"Андрей"}
]}

输出:

Вилен Авангард Станислав Андрей

1 个答案:

答案 0 :(得分:0)

如果您需要使用正则表达式,请检查online script here

一般用途:

您需要检查firstNamelastName是否为英语,如果没有,则将它们存储在一个数组中,最后只是将它们作为结果需求进行内爆。

$json = '{"employees":[
            {"firstName":"John", "lastName":"Doe"},
            {"firstName":"John", "lastName":"Doe"},
            {"firstName":"Вилен", "lastName":"Авангард"},
            {"firstName":"Станислав", "lastName":"Андрей"}
        ]}';
$arr = json_decode($json, true);

$out = array();
foreach($arr['employees'] as $value){
    if(strlen($value['firstName']) != mb_strlen($value['firstName'], 'utf-8'))
        $out[] = $value['firstName'];
    if(strlen($value['lastName']) != mb_strlen($value['lastName'], 'utf-8'))
        $out[] = $value['lastName'];
}

echo implode(" ", $out); //Вилен Авангард Станислав Андрей
  

注意:您需要安装mbstring php模块。