get a link to a search in php

0

I'm doing a php validator initially with a simple function.

$url = $_POST['url'];
$estado;
$amp = file_get_contents("$url");
$buscar = "/amp";
$resul = strpos($amp, $buscar);
if($resul!= false){
    $estado= 'La página existe!';
}else{
    $estado= 'La página no existe :(';
}

echo $estado;

what the function does is to look for this tag in the code of a page

<link rel="amphtml" href="https://dominio.com/amp/version-mobil-de-la-pagina">

if you find it, it means that this page has an amp version available. but I would like you to also throw me only the link of that version, that is:

https://dominio.com/amp/version-mobil-de-la-pagina

I have no idea how to implement it maybe with regular expressions, but I do not know how to start, thanks in advance.

    
asked by ragnamex 13.09.2018 в 22:01
source

1 answer

1

Try the following, which is not more than specifically search the url with the format you are looking for within the content of your page. If you find it, it shows the satisfactory text and the list of all the ones you found.

$url = $_POST['url'];   
$estado;
$amp = file_get_contents($url);
$matches = [];
preg_match_all('/https?:\/\/.+\/amp\/[^"\']+/', $data, $matches);
if(count($matches) > 0){
    echo "La pagina existe <br />";
    print_r(array_shift($matches));
} else {
    echo "La pagina no existe.";
}
    
answered by 13.09.2018 / 22:48
source