<?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; }
You have javascript disabled. You will not be able to edit any code.