Please I need to use php to extract phrases from a text shown below:
Peter,
please check your email for a document I sent, thanks.
Mark,
please I need you to fix my Toyota Camry, thanks.
Mark,
please go and pick up the kids from school, thanks.
Jane,
please go the shop and some items for the house, thanks.
What I need is to display only the phrases relating to Mark. Please note that in this example, Mark has only 2 messages and it could be more residing between Peter and Jane
<?php
$data = "
Peter, <br>
please check your email for a document I sent, thanks.<br>
Mark, <br>
please I need you to fix my Toyota Camry, thanks. <br>
Mark, <br>
please go and pick up the kids from school, thanks. <br>
Jane, <br>
please go the shop and some items for the house, thanks.
";
$start = 'Mark';
$end = 'thanks';
$startIndex = strpos($data, $start);
$endIndex = strpos($data, $end, $startIndex) + strlen($end);
$result = substr($data, $startIndex, $endIndex - $startIndex);
echo $result;
?>
This is what I tried but the result was only the first Phrase
Mark, Please I need to fix my Toyota Camry, thanks.
But I need it to show the second or more phrases relating to Mark.
Please I need help to achieve this.
For this example I am expecting as follows:
Mark, Please I need you to fix my Toyota Camry, thanks. Mark, Please
go and pick up the kids from school, thanks.
2
Answers
Using a while loop to keep going, until the strpos to determine the start index returns false:
You can explode your string by
<br>
and search every two items for the key and append the next if found.