[php] 문자열에 배열의 값이 포함되어 있는지 확인

문자열에 배열에 저장된 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;
}

입력 된 내용에 관계없이 항상 “일치 할 수 없음”이 반환됩니다.

이것이 일을하는 올바른 방법입니까?



답변

이 시도.

$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 ()를 사용 하십시오 .


답변

이 시도:

$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;
}

– 감사


답변

str_replacecount 매개 변수로 간단 하게 여기에서 작동합니다.

$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


답변

원하는 것은 배열에서 문자열을 찾는 것이라면 훨씬 쉬웠습니다.

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


답변

$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)


답변

더 빠른 방법은 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";
}


답변

다음은 주어진 문자열의 배열에서 모든 값을 검색하는 미니 함수입니다. 내 사이트에서이 정보를 사용하여 방문자 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