php 检查字符串是否包含数组中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19445798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if string contains a value in array
提问by danyo
I am trying to detect whether a string contains at least one URL that is stored in an array.
我试图检测一个字符串是否至少包含一个存储在数组中的 URL。
Here is my array:
这是我的数组:
$owned_urls = array('website1.com', 'website2.com', 'website3.com');
The string is entered by the user and submitted via PHP. On the confirmation page I would like to check if the URL entered is in the array.
该字符串由用户输入并通过 PHP 提交。在确认页面上,我想检查输入的 URL 是否在数组中。
I have tried the following:
我尝试了以下方法:
$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;
}
No matter what is inputted the return is always "Match not found".
无论输入什么,返回始终是“找不到匹配项”。
Is this the correct way of doing things?
这是正确的做事方式吗?
回答by Daniele Vrut
Try this.
尝试这个。
$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;
Use stristr()or stripos()if you want to check case-insensitive.
回答by Anand Solanki
Try this:
尝试这个:
$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;
}
- Thanks
- 谢谢
回答by jitendrapurohit
Simple str_replace
with count parameter would work here:
Simple str_replace
with 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.";
}
More Info - https://www.techpurohit.com/extended-behaviour-explode-and-strreplace-php
更多信息 - https://www.techpurohit.com/extended-behaviour-explode-and-strreplace-php
回答by Joseph Philbert
This was a lot easier to do if all you want to do is find a string in an array.
如果您只想在数组中找到一个字符串,那么这会容易得多。
$array = ["they has mystring in it", "some", "other", "elements"];
if (stripos(json_encode($array),'mystring') !== false) {
echo "found mystring";
}
回答by RafH
$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 );
output
输出
bool(true)
回答by vencedor
I think that a faster way is to use preg_match.
我认为更快的方法是使用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";
}
回答by crisc82
Here is a mini-function that search all values from an array in a given string. I use this in my site to check for visitor IP is in my permitted list on certain pages.
这是一个从给定字符串中的数组中搜索所有值的小函数。我在我的网站中使用它来检查访问者 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
}
how to use
如何使用
$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>";
}
DEMO: http://phpfiddle.org/lite/code/qf7j-8m09
演示:http: //phpfiddle.org/lite/code/qf7j-8m09
striposfunction is not very strict. it's not case sensitive or it can match a part of a word http://php.net/manual/ro/function.stripos.php
if you want that search to be case sensitive use strposhttp://php.net/manual/ro/function.strpos.php
for exact match use regex (preg_match), check this guy answer https://stackoverflow.com/a/25633879/4481831
stripos功能不是很严格。它不区分大小写或者它可以匹配一个词的一部分 http://php.net/manual/ro/function.stripos.php
如果您希望该搜索区分大小写,请使用strpos http://php.net/manual/ro/function.strpos.php
对于完全匹配使用正则表达式(preg_match),检查这个人的答案https://stackoverflow.com/a/25633879/4481831
回答by billyonecan
If your $string
is always consistent (ie. the domain name is alwaysat 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).
如果您$string
始终保持一致(即域名始终位于字符串的末尾),您可以使用explode()
with end()
,然后使用in_array()
来检查匹配项(正如@Anand Solanki 在他们的回答中指出的那样)。
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.
如果没有,您最好使用正则表达式从字符串中提取域,然后用于in_array()
检查匹配项。
$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
这是一个演示
回答by Sandesh
$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';
}
回答by revo
You are checking whole string to the array values. So output is always false
.
您正在检查整个字符串到数组值。所以输出总是false
.
I use both array_filter
and strpos
in this case.
在这种情况下,我同时使用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";