使用 PHP 在另一个字符串中搜索特定字符串

发布于 2024-10-03 01:47:14 字数 619 浏览 1 评论 0原文

我需要在 PHP 中搜索一个字符串以查找另一个字符串的出现情况。我有一些我一直在玩的代码,但它似乎不起作用。

这是我的代码:

while (list($key, $val) = each($keyword)) {
  $pos = strpos($location, $val);
  if($pos == false) {
    echo "allow";
    exit;
  } else {
    echo "deny";
    exit;
  }
}

我已经尝试了下面的一些选项,但仍然找不到匹配项。这是我正在搜索的内容:

我需要找到:*

blah

在:

http://blah.com

似乎找不到它。该代码在常规句子中工作:

Today, the weather was very nice.

它会从句子中找到任何单词,但是当它们全部放在一起(在 URL 中)时,它似乎找不到它。

I need to search a string for any occurances of another string in PHP. I have some code that I've been playing with, but it doesn't seem to be working.

Here is my code:

while (list($key, $val) = each($keyword)) {
  $pos = strpos($location, $val);
  if($pos == false) {
    echo "allow";
    exit;
  } else {
    echo "deny";
    exit;
  }
}

I have tried some of the options below, but it still does not find the match. Here is what I'm searching:

I need to find:*

blah

In:

http://blah.com

Nothing seems to find it. The code works in regular sentences:

Today, the weather was very nice.

It will find any word from the sentence, but when it is all together (in a URL) it can't seem to find it.

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

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

发布评论

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

评论(1

难得心□动 2024-10-10 01:47:14

在 php 中检查布尔值 FALSE 时,需要使用 === 运算符。否则,当在字符串的 0 索引位置找到字符串匹配时,您的 if 条件将错误地计算为 true。这在 php 文档 strpos( )

此外,根据您在另一个答案下留下的评论,似乎您需要从允许访问的块中删除 exit 语句。

将它们放在一起,我想您的代码应该如下所示:

while (list($key, $val) = each($keyword)) {
  $pos = strpos($location, $val);
  if($pos === false) { // use === instead of ==
    echo "allow";
  } else {
    echo "deny";
    exit;
  }
}

更新:

根据您提供的新信息,我重写了逻辑:

function isAllowed($location) {
    $keywords = array('blah', 'another-restricted-word', 'yet-another-restricted-word');
    foreach($keywords as $keyword) {
        if (strpos($location, $keyword) !== FALSE) {
            return false;
        }
    }
    return true;
}

$location = 'http://blah.com/';
echo isAllowed($location) ? 'allow' : 'deny';

When checking for boolean FALSE in php, you need to use the === operator. Otherwise, when a string match is found at the 0 index position of a string, your if condition will incorrectly evaluate to true. This is mentioned explicitly in a big red box in the php docs for strpos().

Also, based on a comment you left under another answer, it appears as though you need to remove the exit statement from the block that allows access.

Putting it all together, I imagine your code should look like this:

while (list($key, $val) = each($keyword)) {
  $pos = strpos($location, $val);
  if($pos === false) { // use === instead of ==
    echo "allow";
  } else {
    echo "deny";
    exit;
  }
}

Update:

With the new information you've provided, I've rewritten the logic:

function isAllowed($location) {
    $keywords = array('blah', 'another-restricted-word', 'yet-another-restricted-word');
    foreach($keywords as $keyword) {
        if (strpos($location, $keyword) !== FALSE) {
            return false;
        }
    }
    return true;
}

$location = 'http://blah.com/';
echo isAllowed($location) ? 'allow' : 'deny';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文