Compare a text with a multidimensional array with regular expressions

0

I have sincerely tried with in_array , array_search and I could not do it. I have, for example, the following Array:

$arg = array( 
    "vimeo"=>array(
        "link" => '/https?:\/\/[w\.]*vimeo\.[^\/]*\/([^?]*)/is'
    ),

    "dailymotion"=>array(
        "link" => '/https?:\/\/[w\.]*dailymotion\.[^\/]*\/([^?]*)/is',
    ),
);

I would like to go through the array and find a co-residence, but in all cases it tells me that there are no co-existences. How should I do it?

if(in_array($input, $arg)){
    echo "Hay coincidencias";
}else{
    echo "No hay coincidencias";
    echo " -> ";
    echo $input;

The input takes a URL of a form and I want to know if the link corresponds to one of the two websites

    
asked by C00LMasterN 15.01.2018 в 21:13
source

2 answers

2

If I understand correctly, you have:

  • two regular expressions within an associative array
  • and any URL taken from an input

And from what you see, you want to verify if the URL of the input complies with the regular expressions that are inside your array.

So, one way to do it would be like this:

NOTE: I have modified the original code, evaluating only the link of the array, so that the code is more specific and the logic does not fail if in the future you decide to add more key pairs / value to that array.

<?php

$arg = array( 
                "vimeo"=>      array("link" => '/https?:\/\/[w\.]*vimeo\.[^\/]*\/([^?]*)/is' ),
                "dailymotion"=>array("link" => '/https?:\/\/[w\.]*dailymotion\.[^\/]*\/([^?]*)/is',),
            );

$input="https://www.dailymotion.com/is/video";
compararURL($input,$arg);

$input="https://www.dailymotionsss.com/is/video";
compararURL($input,$arg);

$input="https://www.vimeo.com/is/894regqvideo";
compararURL($input,$arg);

$input="https://www.example.com";
compararURL($input,$arg);



function compararURL($input,$arg){
    foreach ($arg as $k=>$v){
        if (preg_match($v["link"], $input)){
            echo "Hay coincidencias con: ".$v["link"]." ";
            echo $input.PHP_EOL;
        }else{
            echo "No hay coincidencias con ".$v["link"];
            echo " -> ";
            echo $input.PHP_EOL;
        }
    }
}
?>

Test results

No hay coincidencias con /https?:\/\/[w\.]*vimeo\.[^\/]*\/([^?]*)/is -> https://www.dailymotion.com/is/video
Hay coincidencias con: /https?:\/\/[w\.]*dailymotion\.[^\/]*\/([^?]*)/is https://www.dailymotion.com/is/video
No hay coincidencias con /https?:\/\/[w\.]*vimeo\.[^\/]*\/([^?]*)/is -> https://www.dailymotionsss.com/is/video
No hay coincidencias con /https?:\/\/[w\.]*dailymotion\.[^\/]*\/([^?]*)/is -> https://www.dailymotionsss.com/is/video
Hay coincidencias con: /https?:\/\/[w\.]*vimeo\.[^\/]*\/([^?]*)/is https://www.vimeo.com/is/894regqvideo
No hay coincidencias con /https?:\/\/[w\.]*dailymotion\.[^\/]*\/([^?]*)/is -> https://www.vimeo.com/is/894regqvideo
No hay coincidencias con /https?:\/\/[w\.]*vimeo\.[^\/]*\/([^?]*)/is -> https://www.example.com
No hay coincidencias con /https?:\/\/[w\.]*dailymotion\.[^\/]*\/([^?]*)/is -> https://www.example.com
    
answered by 15.01.2018 / 22:02
source
2

One solution could be the following:

  • Use foreach to iterate the array $arg
  • Use preg_match to check if the value of $input meets the regular expression .

Example:

$arg = array(
    "vimeo" => array(
        "link" => '/https?:\/\/[w\.]*vimeo\.[^\/]*\/([^?]*)/is'
     ),
    "dailymotion" => array(
        "link" => '/https?:\/\/[w\.]*dailymotion\.[^\/]*\/([^?]*)/is',
    ),
);
$input = 'https://vimeo.com/channels/staffpicks/250383662';
$match = false;

// Por cada valor en $arg
foreach($arg as $site => $data) {

    // Si el valor de $input cumple la RE de sitio
    if (preg_match($data['link'], $input)) {
        $match = true;
        break;
    }
}

if ($match) {
    echo "Hay coincidencias";
} else {
    echo "No hay coincidencias";
    echo " -> ";
    echo $input;
}
    
answered by 15.01.2018 в 21:58