$longString = "I walk in the park.";
$stringArray = array("walk", "park", "beach");
基本上,我想在$longString
中搜索$stringArray
中的所有子字符串。所以在这种情况下,我想找到步行和停车,然后回应那些字符串。
这可行吗?谢谢你们!
答案 0 :(得分:0)
遍历数组并检查长字符串中是否存在该字符串:
$longString = "I walk in the park and I like to jumprope.";
$stringArray = array("walk" => "walk", "park" => "park", "jumprope" => "jump rope");
foreach( $stringArray as $search => $print) {
if( !(strpos( $longString, $search) === false)) {
echo $print;
}
}
答案 1 :(得分:0)
$longString = "I walk in the park.";
$stringArray = array("walk", "park", "beach");
foreach ($stringArray as $item) {
if (stristr($longString, $item))
echo $item . "<br>";
}
答案 2 :(得分:0)
$longString = "I walk in the park.";
$stringArray = array{"walk", "park", "beach",);
foreach( $stringArray as $search) {
if(strpos( $longString, $search) !== FALSE) {
echo $search;
}
}
答案 3 :(得分:0)
当然,这是可能的:
$longString = "I walk in the park.";
$stringArray = array("walk", "park", "beach");
foreach ($stringArray as $key => $val) {
if (strpos($longString, $val) === false) {
unset($stringArray[$key]);
}
}
var_dump($stringArray);
$stringArray
将仅包含$longString
中显示的字符串。