检查字符串是否包含数组中的值

dan*_*nyo 69 php arrays

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

这是我的数组:

$owned_urls = array('website1.com', 'website2.com', 'website3.com');
Run Code Online (Sandbox Code Playgroud)

该字符串由用户输入并通过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;
}
Run Code Online (Sandbox Code Playgroud)

无论输入什么,返回总是"未找到匹配".

这是正确的做事方式吗?

Dan*_*ara 73

试试这个.

$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;
Run Code Online (Sandbox Code Playgroud)

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

  • 来自手册:`**注意**:如果您只想确定特定针是否出现在haystack中,请使用速度更快且内存更少的函数strpos()代替. (4认同)
  • @danyo如果用户进入像`site3.com`这样的域,这将无效.当它不应该匹配时,它将匹配`mysite3.com` (4认同)
  • 几乎 - 这将回显"未找到匹配",如果列表中的第一个url不匹配则返回false,即使另一个url也不匹配."else"块的内容需要低于`foreach`循环. (3认同)

Ana*_*nki 19

试试这个:

$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;
}
Run Code Online (Sandbox Code Playgroud)

- 谢谢

  • 这假定字符串由空格分隔.例如,它不适用于以下字符串`我的网址是https:// website3.com` (8认同)
  • 甚至不会为'我有website3.com域'工作.这假定字符串在最后,在使用用户提交的文本时,您不能这样做 (4认同)

jit*_*hit 14

简单str_replace的count参数可以在这里工作:

$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.";
}
Run Code Online (Sandbox Code Playgroud)


小智 12

如果你想要做的就是在数组中找到一个字符串,那么这样做要容易得多.

$array = ["they has mystring in it", "some", "other", "elements"];
if (stripos(json_encode($array),'mystring') !== false) {
echo "found mystring";
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为这是**最好的答案**但由于代码中的简单错误而未收到upvotes.@Burgi我编辑了答案,现在它是数组甚至更多,多个子数组和他的方法仍然很好用!! (3认同)
  • 您的输入数组实际上是一个字符串。 (2认同)
  • 如果您要在检查之前将数组转换为字符串,为什么不直接内爆呢?不使用 json_encode() 的一个很好的理由是,如果您的数组中有字符,那么 json_encode() 将会转义或变异。这种突变风险足以成为不建议将此技术作为通用工具的理由。**失败演示:https://3v4l.org/RjPbX** (2认同)

Raf*_*afH 7

$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 );
Run Code Online (Sandbox Code Playgroud)

产量

bool(true)
Run Code Online (Sandbox Code Playgroud)

  • 以供参考; PHP 7.2中不推荐使用create_function。 (2认同)

eli*_*z82 6

这是一个小函数,用于搜索给定字符串中数组中的所有值。我在我的网站中使用它来检查访客 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
}
Run Code Online (Sandbox Code Playgroud)

如何使用

$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>";
}
Run Code Online (Sandbox Code Playgroud)

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


ven*_*dor 5

我认为更快的方法是使用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";
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您提供此代码段,它可能会提供一些有限的即时帮助。[适当的解释](https://meta.stackexchange.com/q/114762)通过显示为什么这是解决问题的好方法,并将大大提高其长期价值,与其他读者一起使它对以后的读者更有用,类似的问题。请[编辑](https://meta.stackoverflow.com/posts/360251/edit)您的答案以添加一些解释,包括您所做的假设。[ref](https://meta.stackoverflow.com/a/360251/8371915) (4认同)