2009-07-17 20 views
5

Por un tiempo he estado buscando un código para obtener una URL fuera de una cadena usando PHP. Básicamente estoy tratando de obtener una URL abreviada de un mensaje, y luego hago una solicitud HEAD para encontrar el enlace real.Obtiene una URL de una cadena

¿Alguien tiene algún código que devuelva las URL de las cadenas?

Gracias de antemano.

Editar para Ghost Dog:

Este es un ejemplo de lo que estoy de análisis: se

$test = "I am testing this application for http://test.com YAY!"; 

Y aquí está la respuesta que obtuve que resolvió:

$regex = '$\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]$i'; 

preg_match_all($regex, $string, $result, PREG_PATTERN_ORDER); 
$A = $result[0]; 

foreach($A as $B) 
{ 
    $URL = GetRealURL($B); 
    echo "$URL<BR>";  
} 


function GetRealURL($url) 
{ 
    $options = array(
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_HEADER   => true, 
     CURLOPT_FOLLOWLOCATION => true, 
     CURLOPT_ENCODING  => "", 
     CURLOPT_USERAGENT  => "spider", 
     CURLOPT_AUTOREFERER => true, 
     CURLOPT_CONNECTTIMEOUT => 120, 
     CURLOPT_TIMEOUT  => 120, 
     CURLOPT_MAXREDIRS  => 10, 
    ); 

    $ch  = curl_init($url); 
    curl_setopt_array($ch, $options); 
    $content = curl_exec($ch); 
    $err  = curl_errno($ch); 
    $errmsg = curl_error($ch); 
    $header = curl_getinfo($ch); 
    curl_close($ch); 
    return $header['url']; 
} 

Vea la respuesta para los detalles.

+0

¿qué tal mostrar un ejemplo de lo que Reescribiendo – ghostdog74

Respuesta

10

Este código puede ser útil (véase el último mensaje de MadTechie):

http://www.phpfreaks.com/forums/index.php/topic,245248.msg1146218.html#msg1146218

<?php 
$string = "some random text http://tinyurl.com/9uxdwc some http://google.com random text http://tinyurl.com/787988"; 

$regex = '$\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]$i'; 

preg_match_all($regex, $string, $result, PREG_PATTERN_ORDER); 
$A = $result[0]; 

foreach($A as $B) 
{ 
    $URL = GetRealURL($B); 
    echo "$URL<BR>"; 
} 


function GetRealURL($url) 
{ 
    $options = array(
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_HEADER   => true, 
     CURLOPT_FOLLOWLOCATION => true, 
     CURLOPT_ENCODING  => "", 
     CURLOPT_USERAGENT  => "spider", 
     CURLOPT_AUTOREFERER => true, 
     CURLOPT_CONNECTTIMEOUT => 120, 
     CURLOPT_TIMEOUT  => 120, 
     CURLOPT_MAXREDIRS  => 10, 
    ); 

    $ch  = curl_init($url); 
    curl_setopt_array($ch, $options); 
    $content = curl_exec($ch); 
    $err  = curl_errno($ch); 
    $errmsg = curl_error($ch); 
    $header = curl_getinfo($ch); 
    curl_close($ch); 
    return $header['url']; 
} 

?> 
+0

Sí, eso era exactamente lo que necesitaba –

2

Algo así como:

$matches = array(); 
preg_match_all('/http:\/\/[a-zA-Z0-9.-]+\/[a-zA-Z0-9.-]+/', $text, $matches); 
print_r($matches); 

que necesitará para sintonizar la expresión regular para obtener exactamente lo que quieres.

Para obtener la URL, considere algo tan simple como:

curl -I http://url.com/path | grep Location: | awk '{print $2}'

+0

sin necesidad grep: curl -I http://url.com/path | awk '/ Location/{print $ 2}' – ghostdog74

Cuestiones relacionadas