I'm trying to get a values from the following array:
$array = ["jack", "name", "father"];
Then i want to get back the line of where the word appears in in the following string:
$story = "Hello, my name is jack and i'm in the army. I've been in the army for 10 years. My father was also in the army.";
The outcome should be:
"Hello, my name is jack and i'm in the army. My father was also in the army.";
<?php
$array = ["jack", "name", "father"];
$story = "Hello, my name is jack and i'm in the army. I've been in the army for 10 years. My father was also in the army.";
$return = [];
$splitStory = explode(".", $story);
$data = array_filter($splitStory);
//echo "<pre>";
//print_r(array_filter($splitStory));
$count = count($data);
$count2 = count($data);
for($i=0; $i < $count; $i++)
{
for($j=0; $j < $count2; $j++)
{
if (strpos($data[$i], $array[$j]) !== false) {
$return[] = $data[$i];
}
}
}
$returnArray = array_unique($return);
$returnStr = implode(".", $returnArray);
echo $returnStr; // Output : Hello, my name is jack and i'm in the army. My father was also in the army
?>