-2

Get Cyrillic words from JSON string in PHP? JSON string is unstructured and any type.

Example:

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

Output:

Вилен Авангард Станислав Андрей
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
Bayasgalan
  • 11
  • 2
  • 6

1 Answers1

0

If you need to do with regular expression the check the online script here

For general purpose:

You need to check the firstName or lastName is english or not, if not then store them in an array and at the end just implode them as the result demand.

$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); //Вилен Авангард Станислав Андрей

Note: You need mbstring php module installed.

Murad Hasan
  • 9,565
  • 2
  • 21
  • 42