3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = "# 12 # # # # Hello World"; $strArr = explode(' ', $str); $strArrKeys = array_keys($strArr, '#'); $strArrSplit = array_pop($strArrKeys) + 1; //all after the last # $subArr = array_slice($strArr, $strArrSplit); echo implode(' ', $subArr); //all before the last # (inluding the #). $subArr = array_slice($strArr, 0, $strArrSplit); echo implode(' ', $subArr);

preferences:
30.37 ms | 402 KiB | 5 Q