3v4l.org

run code in 300+ PHP versions simultaneously
<?php $examples = [ 'The time is over. # its mean I\'m need to die.', 'Please help me. # Ghost. I am here alone.', 'Sorry. # help yourself.']; foreach ($examples as $example) { $exploded = explode('#', $example); $substr = trim(substr($exploded[1], 0, strpos($exploded[1], '.'))); var_dump($substr); } echo "------------------------\n"; //////////////////////////////////////////////////////////////////////////////////////////////////////////// $test = parseString('Sorry. # help yourself.'); function parseString($string) { $exploded = explode('#', $string); $substr = trim(substr($exploded[1], 0, strpos($exploded[1], '.'))); return $substr; } var_dump($test); echo "------------------------\n"; //////////////////////////////////////////////////////////////////////////////////////////////////////////// $stringExample = "The time is over. # its mean I'm need to die. Please help me. # Ghost. I am here alone. Sorry. # help yourself."; $test2 = parseString2($stringExample); function parseString2($string) { $result = []; $array = explode("\n", $string); foreach ($array as $a) { $exploded = explode('#', $a); $substr = trim(substr($exploded[1], 0, strpos($exploded[1], '.'))); $result[] = $substr; } return $result; } var_dump($test2); echo "------------------------\n"; //////////////////////////////////////////////////////////////////////////////////////////////////////////// $stringExample2 = "The time is over. # its mean I'm need to die. Please help me. # Ghost. I am here alone. Sorry. # help yourself."; var_dump(parseString3($stringExample2)); function parseString3($stringExample) { $result2 = []; $startBlock = false; $block = 0; foreach (str_split($stringExample) as $char) { if ($char === '#') { // Start block $startBlock = true; } else if ($startBlock && $char === '.') { // End block $result2[$block] = trim($result2[$block]); // Remove unnecessary whitespace $block++; $startBlock = false; } else if ($startBlock) { // Character to append to block if (!isset($result2[$block])) { $result2[$block] = ''; } $result2[$block] .= $char; } } return $result2; }
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
string(24) "its mean I'm need to die" string(5) "Ghost" string(13) "help yourself" ------------------------ string(13) "help yourself" ------------------------ array(3) { [0]=> string(24) "its mean I'm need to die" [1]=> string(5) "Ghost" [2]=> string(13) "help yourself" } ------------------------ array(3) { [0]=> string(24) "its mean I'm need to die" [1]=> string(5) "Ghost" [2]=> string(13) "help yourself" }

preferences:
153.22 ms | 409 KiB | 5 Q