I need to extract id and name from the following string.
$string = $data[1][0];
preg_match('/NAME\s+:([^ID]+)(ID\s+:)(.+)/', $string, $matches);
print_r($matches);
The above expression works when I have tried online tools but it's not working in the script only when getting value from the array.
String value is
$string = 'NAME : KARL JOHNSON ID : 12345 (LGW FA-319,320 ) ';
ID:if they wanted.\nin the string? With this string you can't really tell what's a key and what's a value.[^ID]+probably does not do what you expect. It's a negated character class and matches characters that are neitherInorD. To find the culprit please show the exact data that fails, as your sample data is actually working with your code. Here is another regex idea.