2009-07-17 6 views
5

Depuis quelque temps, je cherche un code pour extraire les URL d'une chaîne en utilisant PHP. Essentiellement, j'essaie d'obtenir une URL raccourcie d'un message, puis de faire une requête HEAD pour trouver le lien réel.Obtenir une URL à partir d'une chaîne

Quelqu'un at-il du code qui renvoie des URL à partir de chaînes?

Merci d'avance.

Modifier pour Ghost Dog:

Voici un échantillon de ce que je suis analyse:

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

Et voici la réponse que je suis arrivé que l'a résolu:

$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']; 
} 

Voir la réponse pour les détails.

+0

que diriez-vous montrer un exemple de ce que vous un re parsing – ghostdog74

Répondre

10

Ce code peut être utile (voir le dernier message 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

Ouais, c'était exactement ce dont j'avais besoin –

2

Quelque chose comme:

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

Vous aurez besoin de régler la regexp pour obtenir exactement ce que vous voulez.

Pour obtenir l'URL, réfléchissez à quelque chose d'aussi simple que:

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

+0

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

Questions connexes