我正在尝试从以下数组中获取值:
$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.";
结果应该是:
"Hello, my name is jack and i'm in the army. My father was also in the army.";
答案 0 :(得分:1)
<?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
?>