3v4l.org

run code in 300+ PHP versions simultaneously
<?php $regex = '^(?=.{5}$) .*? ( (?<high_straight>(?!.*(?<hscard>.).*(?P=hscard))[TJQKA]{5})| (?<straight>(?!.*(?<scard>.).*(?P=scard))(?:[A2345]{5}|[23456]{5}|[34567]{5}|[45678]{5}|[56789]{5}|[6789T]{5}|[789TJ]{5}|[89TJQ]{5}|[9TJQK]{5}))| (?<four_of_a_kind>.)(?:.*(?P=four_of_a_kind)){3}| (?<full_house>(?=.*(?<fh_card1>.)(?=(?:.*(?P=fh_card1)){2}))(?=.*(?<fh_card2>(?!(?P=fh_card1)).)(?=.*(?P=fh_card2))).+)| (?<three_of_a_kind>.)(?:.*(?P=three_of_a_kind)){2}| (?<two_pair>(?=.*(?<tp_card1>.)(?=(?P=tp_card1)))(?=.*(?<tp_card2>(?!(?P=tp_card1)).)(?=.*(?P=tp_card2))).+)| (?<two_of_a_kind>.).*(?P=two_of_a_kind) )'; $hands = array('AJKTQ', '43A52', 'AAKAA', '58888', '58585', 'AKKKA', '22333', 'AK9AA', '78444', '93233', '64886', '662TT', '67898', '432A4', 'KQA34', '34628'); foreach ($hands as $hand) { echo "$hand: "; preg_match_all("/$regex/x", $hand, $matches, PREG_SET_ORDER); if (!count($matches)) { // here you should check for a flush echo "high card"; } else { $matched_hand = array_filter($matches[0], function ($v, $k) { return !empty($v) && !is_numeric($k); }, ARRAY_FILTER_USE_BOTH); switch (key($matched_hand)) { case 'full_house': echo "full house {$matches[0]['fh_card1']} over {$matches[0]['fh_card2']}"; break; case 'two_pair': // here you should sort for the highest pair echo "two_pair {$matches[0]['tp_card1']} over {$matches[0]['tp_card2']}"; break; case 'four_of_a_kind': case 'three_of_a_kind': case 'two_of_a_kind': echo key($matched_hand) . " " . current($matched_hand); break; default: // here you should check for a straight flush or royal flush echo key($matched_hand); break; } } echo "\n"; }
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.24, 7.3.0 - 7.3.33, 7.4.12 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
AJKTQ: high_straight 43A52: straight AAKAA: four_of_a_kind A 58888: four_of_a_kind 8 58585: full house 5 over 8 AKKKA: full house K over A 22333: full house 3 over 2 AK9AA: three_of_a_kind A 78444: three_of_a_kind 4 93233: three_of_a_kind 3 64886: two_pair 8 over 6 662TT: two_pair T over 6 67898: two_of_a_kind 8 432A4: two_of_a_kind 4 KQA34: high card 34628: high card

preferences:
150.38 ms | 408 KiB | 5 Q