93

我试图检测一个字符串是否包含至少一个存储在数组中的 URL。

这是我的数组:

$owned_urls = array('website1.com', 'website2.com', 'website3.com');

该字符串由用户输入并通过 PHP 提交。在确认页面上,我想检查输入的 URL 是否在数组中。

我尝试了以下方法:

$string = 'my domain name is website3.com';
if (in_array($string, $owned_urls))
{
    echo "Match found"; 
    return true;
}
else
{
    echo "Match not found";
    return false;
}

无论输入什么,返回总是“未找到匹配”。

这是正确的做事方式吗?

4

15 回答 15

100

尝试这个。

$string = 'my domain name is website3.com';
foreach ($owned_urls as $url) {
    //if (strstr($string, $url)) { // mine version
    if (strpos($string, $url) !== FALSE) { // Yoshi version
        echo "Match found"; 
        return true;
    }
}
echo "Not found!";
return false;

如果要检查不区分大小写,请使用stristr()stripos() 。

于 2013-10-18T09:16:43.000 回答
24

尝试这个:

$owned_urls= array('website1.com', 'website2.com', 'website3.com');

$string = 'my domain name is website3.com';

$url_string = end(explode(' ', $string));

if (in_array($url_string,$owned_urls)){
    echo "Match found"; 
    return true;
} else {
    echo "Match not found";
    return false;
}

- 谢谢

于 2013-10-18T09:28:12.960 回答
24

如果您只想在数组中找到一个字符串,这会容易得多。

$array = ["they has mystring in it", "some", "other", "elements"];
if (stripos(json_encode($array),'mystring') !== false) {
echo "found mystring";
}
于 2015-11-29T20:51:41.223 回答
21

简单str_replace的计数参数将在这里工作:

$count = 0;
str_replace($owned_urls, '', $string, $count);
// if replace is successful means the array value is present(Match Found).
if ($count > 0) {
  echo "One of Array value is present in the string.";
}

更多信息 - https://www.techpurohit.com/extended-behaviour-explode-and-strreplace-php

于 2016-11-11T07:27:21.680 回答
8
$string = 'my domain name is website3.com';
$a = array('website1.com','website2.com','website3.com');

$result = count(array_filter($a, create_function('$e','return strstr("'.$string.'", $e);')))>0; 
var_dump($result );

输出

bool(true)
于 2013-10-18T09:31:34.257 回答
7

我认为更快的方法是使用preg_match

$user_input = 'Something website2.com or other';
$owned_urls_array = array('website1.com', 'website2.com', 'website3.com');

if ( preg_match('('.implode('|',$owned_urls_array).')', $user_input)){
    echo "Match found"; 
}else{
    echo "Match not found";
}
于 2018-01-15T13:25:37.073 回答
4

您可以使用 implode 和分隔符 | 连接数组值 然后使用 preg_match 搜索该值。

这是我想出的解决方案...

$emails = array('@gmail', '@hotmail', '@outlook', '@live', '@msn', '@yahoo', '@ymail', '@aol');
$emails = implode('|', $emails);

if(!preg_match("/$emails/i", $email)){
 // do something
}
于 2020-02-14T07:55:40.570 回答
4

这是一个从给定字符串中的数组中搜索所有值的迷你函数。我在我的网站中使用它来检查访问者 IP 是否在某些页面的允许列表中。

function array_in_string($str, array $arr) {
    foreach($arr as $arr_value) { //start looping the array
        if (stripos($str,$arr_value) !== false) return true; //if $arr_value is found in $str return true
    }
    return false; //else return false
}

如何使用

$owned_urls = array('website1.com', 'website2.com', 'website3.com');

//this example should return FOUND
$string = 'my domain name is website3.com';
if (array_in_string($string, $owned_urls)) {
    echo "first: Match found<br>"; 
}
else {
    echo "first: Match not found<br>";
}

//this example should return NOT FOUND
$string = 'my domain name is website4.com';
if (array_in_string($string, $owned_urls)) {
    echo "second: Match found<br>"; 
}
else {
    echo "second: Match not found<br>";
}

演示: http: //phpfiddle.org/lite/code/qf7j-8m09

于 2016-05-24T06:42:07.283 回答
3

If your $string is always consistent (ie. the domain name is always at the end of the string), you can use explode() with end(), and then use in_array() to check for a match (as pointed out by @Anand Solanki in their answer).

If not, you'd be better off using a regular expression to extract the domain from the string, and then use in_array() to check for a match.

$string = 'There is a url mysite3.com in this string';
preg_match('/(?:http:\/\/)?(?:www.)?([a-z0-9-_]+\.[a-z0-9.]{2,5})/i', $string, $matches);

if (empty($matches[1])) {
  // no domain name was found in $string
} else {
  if (in_array($matches[1], $owned_urls)) {
    // exact match found
  } else {
    // exact match not found
  }
}

The expression above could probably be improved (I'm not particularly knowledgeable in this area)

Here's a demo

于 2013-10-18T09:57:39.937 回答
1
$owned_urls= array('website1.com', 'website2.com', 'website3.com');
    $string = 'my domain name is website3.com';
    for($i=0; $i < count($owned_urls); $i++)
    {
        if(strpos($string,$owned_urls[$i]) != false)
            echo 'Found';
    }   
于 2013-10-18T09:45:18.667 回答
1

您正在检查整个字符串到数组值。所以输出总是false.

在这种情况下,我同时使用array_filter和。strpos

<?php
$urls= array('website1.com', 'website2.com', 'website3.com');
$string = 'my domain name is website3.com';
$check = array_filter($urls, function($url){
    global $string;
    if(strpos($string, $url))
        return true;
});
echo $check?"found":"not found";
于 2013-10-18T09:45:41.857 回答
0

我发现这快速简单,无需运行循环。

$array = array("this", "that", "there", "here", "where");
$string = "Here comes my string";
$string2 = "I like to Move it! Move it";

$newStr = str_replace($array, "", $string);

if(strcmp($string, $newStr) == 0) {
    echo 'No Word Exists - Nothing got replaced in $newStr';
} else {
    echo 'Word Exists - Some Word from array got replaced!';
}

$newStr = str_replace($array, "", $string2);

if(strcmp($string2, $newStr) == 0) {
    echo 'No Word Exists - Nothing got replaced in $newStr';
} else {
    echo 'Word Exists - Some Word from array got replaced!';
}

小解释!

  1. $newStr用原始字符串数组中的替换值创建新变量。

  2. 进行字符串比较 - 如果值为 0,则表示字符串相等且没有任何内容被替换,因此字符串中不存在数组中的值。

  3. 如果是 2 的反之亦然,即在进行字符串比较时,原始字符串和新字符串都不匹配,这意味着有东西被替换了,因此数组中的值存在于字符串中。

于 2018-01-16T12:04:11.007 回答
0

我想出了这个对我有用的功能,希望这对某人有帮助

$word_list = 'word1, word2, word3, word4';
$str = 'This string contains word1 in it';

function checkStringAgainstList($str, $word_list)
{
  $word_list = explode(', ', $word_list);
  $str = explode(' ', $str);

  foreach ($str as $word):
    if (in_array(strtolower($word), $word_list)) {
        return TRUE;
    }
  endforeach;

  return false;
}

另外,请注意,如果匹配的单词是其他单词的一部分,则使用 strpos() 的答案将返回 true。例如,如果单词列表包含“st”并且您的字符串包含“street”,则 strpos() 将返回 true

于 2020-03-15T15:07:28.737 回答
0
    $message = "This is test message that contain filter world test3";

    $filterWords = array('test1', 'test2', 'test3');

    $messageAfterFilter =  str_replace($filterWords, '',$message);

    if( strlen($messageAfterFilter) != strlen($message) )
        echo 'message is filtered';
    else
        echo 'not filtered';
于 2017-10-23T14:38:59.567 回答
0
  $search = "web"
    $owned_urls = array('website1.com', 'website2.com', 'website3.com');
          foreach ($owned_urls as $key => $value) {
         if (stristr($value, $search) == '') {
        //not fount
        }else{
      //found
       }

这是搜索任何子字符串、不区分大小写且快速的最佳方法

就像我的mysql一样

前任:

select * from table where name = "%web%"

于 2020-02-06T09:23:09.173 回答