- substr: documentation ( source)
- strpos: documentation ( source)
- printf: documentation ( source)
<?php
$str = "The time is over. # its mean I'm need to die. Please help me. # Ghost. I am here alone. Sorry. # help yourself.";
function lines(string $str): generator {
$hPos = strpos($str, "#");
if($hPos !== false) {
$dPos = strpos($str, ".", $hPos);
if($dPos !== false) {
yield substr($str, $hPos + 1, $dPos - $hPos - 1);
foreach(lines(substr($str, $dPos + 1)) as $line) {
yield $line;
}
}
}
}
foreach(lines($str) as $line) {
printf("%s\n", $line);
}