Capturing a given link in an array

Asked

Viewed 37 times

0

I need to check and capture links that contain (youtube.com/embed) in an array. The position of the link in the array is always variable. How should I do?

Array
(
    [0] => esqueci
    [1] => de
    [2] => entrar
    [3] => no
    [4] => link
    [5] => https://www.youtube.com/embed/zHNOQpl00_I
    [6] => para
    [7] => poder
    [8] => testar,
    [9] => https://www.google.com,
    [10] => https://terra.com.br,
    [11] => https://www.youtube.com/videos/asdasd
    [12] => 

)

Thanks in advance,

  • Item 11 enters capture or not?

  • No... I need to check and get only the links that are youtube with embed. =/

1 answer

0

use for to go through his array, and use the function strpos()

for($i = 0; $i < $array.length; $i++){

  // valor é um item do array
  $findme   = 'youtube.com/embed';
  $pos = strpos($array[$i], $findme);

    if ($pos === false) {
     echo "A string '$findme' não foi encontrada no array '$array[$i]'";
    } else {
      echo "A string '$findme' foi encontrada na string '$array[$i]'";
      // aqui $array[$i] estara sua url.
      echo " e existe na posição $pos"; 
    }

}
  • If I send search the entire link works, but if it is a part of the link ex youtube.com/embed there gives error. =/

  • @Diegoalbuquerque I edited, I believe that now is what I needed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.