0

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.

6
  • Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. Then edit your question to include your source code as a working minimal reproducible example, which can be tested by others. Also see meta.stackoverflow.com/questions/271055/… for SQL related questions. Please see: What Do You Mean “It Doesn't Work”? Commented Nov 4, 2021 at 12:08
  • Where are you defining $url Commented Nov 4, 2021 at 12:10
  • 1
    I find it very weird that all the keys of the array are 0, it should be 0, 1, 2, 3..., not all the same. Commented Nov 4, 2021 at 12:12
  • @DarkBee $url is get_permalink() wordpress function. I call this function check_links($url) in footer and pass $url value I got with get_permalink() Commented Nov 4, 2021 at 12:16
  • @Armando error when copying Commented Nov 4, 2021 at 12:18

1 Answer 1

1

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 = array_map('trim', explode(',',$db_links)); 

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

it will work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.