简体   繁体   中英

Check if url exist in array list of URLS retrieved from database

I am trying to compare the current URL with the URLs retrieved from the database but for some reason only works if it matches the first array.

   $url = get_permalink();
function check_links($url){
    $db_links= get_option('db_links'); 
         
    $exclude = explode(',',$db_links); 

    if(in_array($url, $exclude)){
          echo "display:none"; 
        } 

}

After I dump $exclude, I get

array(3){
    [0]=>string(16) "https://test.com"
    [1]=>string(18) "https://test.com/2"
    [2]=>string(19) "https://test.com/22"
    [3]=>string(20) "https://test.com/235"
} 

I want to compare my current URL with the list above. If it shows I want to echo "display:none"; My current URL is https://test.com/22

if(in_array($url, $exclude)){
  echo "display:none"; 
} 

I found out that it works only if my URL is https://test.com , for other cases it doesn't work.

In this case your string have some \\n and space than not showed and which cause errors, for handeling this errors you most replace the \\n\\ and space with nothing, folow mycode:

$url = get_permalink();
function check_links($url){
    $db_links= get_option('db_links'); 
    $exclude = str_replace("\n","",$exclude);
    $exclude = str_replace(" ","",$exclude);
    $exclude = explode(',',$db_links); 

    if(in_array($url, $exclude)){
          echo "display:none"; 
        }
}

it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM