我试图检测字符串是否包含至少一个存储在数组中的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().
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)
- 谢谢
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)
$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)
这是一个小函数,用于搜索给定字符串中数组中的所有值。我在我的网站中使用它来检查访客 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
stripos功能不是很严格。它不区分大小写,或者可以匹配单词的一部分 http://php.net/manual/ro/function.stripos.php
如果您希望搜索区分大小写,请使用strpos http://php.net/manual/ro/function.strpos.php
对于精确匹配使用正则表达式(preg_match),请检查这个人的答案/sf/answers/1794371561/
我认为更快的方法是使用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)