如何在句子中查找单词,使用字符串或其他 php
how to find word in sentence, use string or another at php
查找单词在句子中的位置,示例:句子:我喜欢 manggo juice 和苹果 果汁。 查找单词 : juice 答案是单词"juice"在位置 4 和 7
找到
之前,我使用了 strpos,但答案是 -> 在 14 和 30 处找到的单词 "juice"。我想要这个 -> 在 4 和 7 处找到 "juice" 这个词。请帮助..真的真的帮助我.. 因为,我是 php.
的新手
只需使用 explode
函数根据空格拆分您的输入。遍历数组,然后在找到字符串 juice
的位置打印索引号 + 1。
$string = "i like manggo juice and apple juice";
$parts = explode(" ", $string);
foreach ($parts as $key => $value) {
if ($value === "juice")
{
echo ($key + 1)."\n";
}
}
查找单词在句子中的位置,示例:句子:我喜欢 manggo juice 和苹果 果汁。 查找单词 : juice 答案是单词"juice"在位置 4 和 7
找到之前,我使用了 strpos,但答案是 -> 在 14 和 30 处找到的单词 "juice"。我想要这个 -> 在 4 和 7 处找到 "juice" 这个词。请帮助..真的真的帮助我.. 因为,我是 php.
的新手只需使用 explode
函数根据空格拆分您的输入。遍历数组,然后在找到字符串 juice
的位置打印索引号 + 1。
$string = "i like manggo juice and apple juice";
$parts = explode(" ", $string);
foreach ($parts as $key => $value) {
if ($value === "juice")
{
echo ($key + 1)."\n";
}
}