Luke B Luke B - 1 year ago 104
PHP Question

Regex to match exact string between two delimeters

I need to match from a string like the following:


Basically, I want to match the exact text after a comma but before a vertical bar. So 'B661' will match. 'B66' alone will NOT match. Can't seem to figure this out!

Jan Jan
Answer Source

Either explode() it twice(!) as @Wiktor said;


$string = "27,B661|27,071315S|27,B2843|2,355516|27,ASB844|27,H994|28,073126|";

$arr = explode("|", $string);
foreach ($arr as $item) {
    list($key, $value) = explode(",", $item);
    echo "$key: $value\n";


Or with a regex like:


See a demo on

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download