在php中搜索数组中的子字符串

发布于 2024-12-15 00:53:09 字数 481 浏览 0 评论 0原文

我正在尝试搜索字符串中某些单词的出现并将它们与数组中的短列表进行匹配。我将通过一个例子来解释我的问题。

            $arrTitle = explode(" ", "Suburban Apartments");
    foreach( $arrTitle as $key => $value){
        echo "Name: $key, Age: $value <br />";
        $words = array("hotel", "apart", "hostel");
        if (in_array($value, $words)) {
            echo "Exists";
        }
    }

我希望将上面示例中的字符串的一部分“Apartments”与 $words 数组进行比较,以查找字符串“Apart”的出现情况。 我希望这是有道理的。这有可能吗?

谢谢

I'm trying to search for the occurrence of certain words in a string and match them with a short list in an array. I'll explain my question by means of an example.

            $arrTitle = explode(" ", "Suburban Apartments");
    foreach( $arrTitle as $key => $value){
        echo "Name: $key, Age: $value <br />";
        $words = array("hotel", "apart", "hostel");
        if (in_array($value, $words)) {
            echo "Exists";
        }
    }

I wish to compare a part of the string, in the above example 'Apartments' against the $words array for the occurrence of the string 'Apart'ments.
I hope this makes sense. Is this possible at all?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋凉 2024-12-22 00:53:09

然后你必须稍微改变一下你的代码。请看下面的内容:

$arrTitle = explode(" ", "Suburban Apartments");
foreach( $arrTitle as $key => $value){
    echo "Name: $key, Age: $value <br />";
    $words = array("hotel", "apart", "hostel");
    foreach($words as $word){
        if (stripos($value, $word) !== false) {
            echo "Exists";
        }
    }
}

注意要搜索的单词上添加的 foreach 以及 strpos 的 in_array 替换。

对于您的个人信息,in_array 检查 $word 是否作为整个值存在于数组中,这不是您想要的。您需要使用 strpos 或 stripos (不区分大小写)检查每个单词是否存在...

Then you'll have to change a bit your code. Look at this below:

$arrTitle = explode(" ", "Suburban Apartments");
foreach( $arrTitle as $key => $value){
    echo "Name: $key, Age: $value <br />";
    $words = array("hotel", "apart", "hostel");
    foreach($words as $word){
        if (stripos($value, $word) !== false) {
            echo "Exists";
        }
    }
}

Note the added foreach on the words to search and the in_array replacement for strpos.

For your personnal information, in_array checks if the $word is in the array as a whole value, which is not what you wanted. You need to check for the existence of each word using strpos or stripos (case insensitive)...

┼── 2024-12-22 00:53:09

使用正则表达式来匹配文本。

http://php.net/manual/en/function.preg-match.php

您需要一次循环遍历每个要查找的数组值,因此您必须在循环内循环并对每个值运行正则表达式来查找匹配项。

确保它们不区分大小写。

Use regular expressions to match the text.

http://php.net/manual/en/function.preg-match.php

You'll need to cycle through each of your sought array values one at a time so you'll have to loop inside your loop and run the regexp on each one looking for a match.

Make sure to make them case insensitive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文