我正在尝试检测字符串是否包含至少一个存储在数组中的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;
}
无论输入什么,返回总是“未找到匹配”。
这是正确的做事方式吗?
答案 0 :(得分:64)
试试这个。
$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;
答案 1 :(得分:17)
试试这个:
$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;
}
- 感谢
答案 2 :(得分:11)
如果您只想在数组中找到一个字符串,那么这样做会容易得多。
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(daoAuthenticationProvider());
auth.ldapAuthentication()
.userSearchBase(userSearchBase)
.groupSearchBase(groupSearchBase)
.userSearchFilter(userSearchFilter)
.userDetailsContextMapper(new DaoUserDetailsContextMapper())
.contextSource().url(url+"/"+base)
.managerPassword(managerPassword)
.managerDn(managerDn);
}
答案 3 :(得分:10)
带有count参数的简单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
答案 4 :(得分: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 );
<强>输出强>
bool(true)
答案 5 :(得分:4)
我认为更快捷的方法是使用 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";
}
答案 6 :(得分:3)
如果您的$string
始终保持一致(即字符串末尾的域名始终),则explode()
与end()
一起使用,然后使用in_array()
检查匹配(正如@Anand Solanki在回答中所指出的那样)。
如果没有,最好使用正则表达式从字符串中提取域名,然后使用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
}
}
上面的表达可能会有所改善(我在这个领域并不是特别了解)
答案 7 :(得分:2)
您可以使用爆破和|的分隔符来连接数组值。 然后使用preg_match搜索值。
这是我想出的解决方案...
$emails = array('@gmail', '@hotmail', '@outlook', '@live', '@msn', '@yahoo', '@ymail', '@aol');
$emails = implode('|', $emails);
if(!preg_match("/$emails/i", $email)){
// do something
}
答案 8 :(得分:2)
这是一个迷你函数,可以搜索给定字符串中数组的所有值。 我在我的网站上使用此功能来检查访问者IP是否在某些页面上的允许列表中。
function array_in_string($str, array $arr) {
foreach($arr as $arr_value) { //start looping the array
if (strpos($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>";
}
DEMO:http://phpfiddle.org/lite/code/qf7j-8m09
strpos功能不是很严格。它不区分大小写,也可以匹配单词的一部分。 http://php.net/manual/ro/function.strpos.php 如果你希望搜索更严格,你必须使用不同的功能(例如,检查这个家伙的答案是否有严格的功能https://stackoverflow.com/a/25633879/4481831)
答案 9 :(得分: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';
}
答案 10 :(得分: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";
答案 11 :(得分: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
答案 12 :(得分: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一样
例如:
从名称=“%web%”的表中选择*
答案 13 :(得分: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!';
}
小解释!
创建新变量,$newStr
替换原始字符串数组中的值。
进行字符串比较 - 如果value为0,则表示字符串相等且未替换任何内容,因此字符串中不存在数组值。
如果反之亦然2,即在进行字符串比较时,原始字符串和新字符串都不匹配,这意味着,某些内容被替换,因此数组中的值存在于字符串中。
< / LI> 醇>答案 14 :(得分: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';
答案 15 :(得分:0)
如果您正在尝试获得精确的单词匹配(在网址中没有路径)
$string = 'my domain name is website3.com';
$words = explode(' ', $string);
$owned_urls= array('website1.com', 'website2.com', 'website3.com');
var_dump(array_intersect($words, $owned_urls));
输出:
array(1) { [4]=> string(12) "website3.com" }
答案 16 :(得分:0)
您没有正确使用in_array(http://php.net/manual/en/function.in-array.php)函数:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
$ needle必须在数组中有一个值,因此首先需要从字符串中提取url(例如使用正则表达式)。像这样:
$url = extrctUrl('my domain name is website3.com');
//$url will be 'website3.com'
in_array($url, $owned_urls)
答案 17 :(得分:-3)
感谢此 - 只是能够使用原始问题的答案来开发一个简单易用的404错误页面检查器,用于自定义404错误页面。
这里是:
您的站点中需要一个livePages数组,通过数组/数据库等,甚至您的<dir>
树的列表也会通过修改来执行此操作:
使用原始IDEA,但使用类似文本而不是strpos, - 这使您可以搜索LIKE名称,因此也允许使用TYPOS,因此您可以避免或找到类似Sound-a和Look-a像名字......
<?php
// We need to GRAB the URL called via the browser ::
$requiredPage = str_replace ('/', '',$_SERVER[REQUEST_URI]);
// We need to KNOW what pages are LIVE within the website ::
$livePages = array_keys ($PageTEXT_2col );
foreach ($livePages as $url) {
if (similar_text($requiredPage, $url, $percent)) {
$percent = round($percent,2); // need to avoid to many decimal places ::
// if (strpos($string, $url) !== FALSE) { // Yoshi version
if (round($percent,0) >= 60) { // set your percentage of "LIKENESS" higher the refiner the search in your array ::
echo "Best Match found = " . $requiredPage . " > ,<a href='http://" . $_SERVER['SERVER_NAME'] . "/" . $url . "'>" . $url . "</a> > " . $percent . "%";
return true;
}
}
}
echo "Sorry Not found = " . $requiredPage;
return false;
?>
希望这有助于某人,就像本文帮助我在404ErrorDoc页面上创建一个非常简单的搜索/匹配。
页面的设计将使服务器能够通过浏览器向任何被调用的URL提出可能的URL匹配...
它工作 - 而且很简单,也许有更好的方法来做到这一点,但这种方式有效。