Link to home
Start Free TrialLog in
Avatar of detox1978
detox1978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP: find URL in a string

Hi All,


I have a string that contains an IMDB URL, the last folder varies e.g.

http://www.imdb.com/title/tt0892318/
http://www.imdb.com/title/tt0955308/

does anyone know how i can extract the URL?
Avatar of Maverickerko
Maverickerko

here is regexp for this url

(http://)*(www.|)*(imdb.com/title/)(\S*)(\/)

and code is
$url = "http://www.imdb.com/title/tt0892318/";
$pattern = '#^(http://)*(www.|)*(imdb.com/title/)(\S*)(\/)#';
$match = preg_match($pattern, $url, $matches);

print_r($matches);
?>


you have 0 or 1 in $match when url is found and id of title you have in $matches[4]
Avatar of Marco Gasi
Try using $_SERVER['SCRIPT_NAME'] (http://php.net/manual/en/reserved.variables.server.php).

Hope this helps
Here's an alternate version.  Took me longer to check it.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Extract</title>
</head>
<body>
<h1>PHP Extract</h1>

<?php 
$teststr = "This that and the http://www.imdb.com/title/tt0892318/ in Southern LA.";

$tstart = strpos($teststr,"http://");
$tend = strpos($teststr," ",$tstart);

$tdat = substr($teststr,$tstart,($tend-$tstart));
echo $teststr."<br>";
echo $tdat."<br>";
 ?>
</body>
</html>

Open in new window

Avatar of detox1978

ASKER

I want to extract the URL, but dont know the tt folder as it will vary each time it is run.
The code I posted extracts any url that starts with "http://".  If you need to extract more than one instance in a string you can use the 'substr' function to get the string remaining after the last URL and do it again.
Thanks Dave, your code works, but there isn't always a space at the end of the URL
my solution works for vary tt folder ... i
Maverickerko, i dont see where my string fits into your snippet.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks.

used your code to make the following

$imdb = substr(strstr($string,'http://www.imdb.com/title/'),0,strpos(strstr($string,'http://www.imdb.com/title/'),'/',26)+1);
Thanks and that's cool.  I just never combine things that way because it makes my eyes cross to look at it.!