3v4l.org

run code in 300+ PHP versions simultaneously
<?php mb_internal_encoding('UTF-8'); function get_goods_info($str) { $ret = array(); if (($sharp_pos = strpos($str, '#')) === false) { return false; } $ret['description'] = substr($str, 0, $sharp_pos); $str = substr($str, $sharp_pos + 1); if (($iro_pos = mb_strpos($str, '色')) === false) { return false; } $ret['color'] = str_replace('/', '色 ', ltrim(mb_substr($str, 0, $iro_pos + 1))); $ret['weight'] = ltrim(mb_substr($str, $iro_pos + 1)); return $ret; } var_dump( get_goods_info('光の3原色から選べます# 赤/青/黄色 210g') /* get_goods_info('# 茶/カーキ/黄/緑色 100g'), get_goods_info('1色でもOK・なんとなくkgにも対応# 肌色 2.5kg'), get_goods_info('これには# マッチしません ')*/ );
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 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.6
array(3) { ["description"]=> string(31) "光の3原色から選べます" ["color"]=> string(20) "赤色 青色 黄色" ["weight"]=> string(4) "210g" }

preferences:
246.52 ms | 406 KiB | 401 Q