tags:

views:

473

answers:

2

For a little while now I've been searching for a code to get URL's out of a string using PHP. I'm basically trying to get a Shortened URL out of a message, and then later do a HEAD request to find the actual link.

Anyone have any code that returns URLs from strings?

Thanks in advanced.

Edit for Ghost Dog:

Here is a sample of what I am parsing:

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

And here is the response I got that solved it:

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

See Answer for the Details.

+1  A: 

Something like:

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

You'll need to tune the regexp to get exactly what you want.

To get the URL out, consider something as simple as:

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

gahooa
no need grep : curl -I http://url.com/path | awk '/Location/{print $2}'
ghostdog74
+6  A: 

This code may be helpful (see MadTechie's latest post):

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

?>
jimyi
Yeah, That was exactly what I needed
Chacha102