3v4l.org

run code in 300+ PHP versions simultaneously
<?php $desc = "GLOVE EXAM BEST TCH ALOE LTX MED 100/BX 10BX/CS"; $uom = "EA"; $keywords = explode(" ", $desc); $pkgstring = ""; $result = []; foreach ($keywords as $key => $keyword){ $firstchar = substr($keyword, 0, 1); if (is_numeric($firstchar)) { $pkgstring .= $keyword.","; } } $pkgstring = substr($pkgstring, 0, -1); $result['pkgstring'] = $pkgstring; $str_ele = explode(",", $pkgstring); $qty_uom = 1; $new_arr[0] = ""; $pos = 1; if (stripos($pkgstring, $uom) !== false){ // sort each element in the best order to execute the rest of the function foreach ($str_ele as $key => $element) { if (stripos($element, '/') !== false) { $x = stripos($element, '/'); $chk = substr($element, $x-1, 1); if (is_numeric($chk)) { $new_arr[0] = $element; } else { $new_arr[$pos] = $element; $pos++; } } } // do the math to get the qty/uom foreach ($new_arr as $key => $str) { echo $str."\n\n"; if (stripos($str, '/'.$uom) !== false) { $size = preg_replace('/[^0-9]/', '', $str); $qty_uom *= $size; break; } else { $size = preg_replace('/[^0-9]/', '', $str); $qty_uom *= $size; } } } $result['qty_uom'] = $qty_uom; print_r($result);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Array ( [pkgstring] => 100/BX,10BX/CS [qty_uom] => 1 )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Array ( [pkgstring] => 100/BX,10BX/CS [qty_uom] => 1 )

preferences:
186.13 ms | 402 KiB | 204 Q