I get string:

$res = "RESULT: SUCCESS
NUMBER_TYPE:    0";

I want get "RESULT" and for this i use explode:

$arr = explode('\n\r', $res);
$result = trim(explode('RESULT:', $arr[0]));

But i get Arrayfalse

Tell me please how correctly get RESULT?

P.S.: result should been SUCCESS

有帮助吗?

解决方案

You can use explode then preg_split

$arr = explode(':', $res);
$arr = preg_split('/\s+/', $arr[1]);
echo $arr[1];

You can also use this one with explode only.

$arr = explode("\n", $res);
$arr= explode(":", $arr[0]);
echo trim($arr[1]);

其他提示

Trying Doing this

$arr = explode(':', $res);
$result = explode(' ', $arr[1]);
print_r($result[0]);

Try :

$ex = explode(":",$res);
$result = explode("\n",$ex[1]);
$result = $result[0];
echo trim($result); //echoes SUCCESS

Or :

$ex = explode("\n", $res);
$result = explode(":", $ex[0]);
$result = $result[1];
echo trim($result); //echoes SUCCESS

Demo 1

Demo 2

The first comment to your answer is the solution. But - for any reason - if you want to get all the keys and values, you can use preg_match for each line. A quick example:

<?php

$res = "RESULT: SUCCESS
NUMBER_TYPE:    0";

$arr = explode("\n", $res);

foreach($arr as $line)
{
    if(preg_match('/^([A-Z_]+):\s+?([a-zA-Z0-9_]+)$/', $line, $matches))
    {
        echo "Key: " . $matches[1] . " - Value: " . $matches[2] . "\n";
    }
}

Output:

Key: RESULT - Value: SUCCESS
Key: NUMBER_TYPE - Value: 0
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top