Solved

PHP: find URL in a string

Posted on 2010-09-01
11
653 Views
Last Modified: 2012-06-22
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?
0
Comment
Question by:detox1978
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 2

Expert Comment

by:Maverickerko
ID: 33580337
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]
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33580362
Try using $_SERVER['SCRIPT_NAME'] (http://php.net/manual/en/reserved.variables.server.php).

Hope this helps
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33580363
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:detox1978
ID: 33580370
I want to extract the URL, but dont know the tt folder as it will vary each time it is run.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33580422
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.
0
 
LVL 2

Author Comment

by:detox1978
ID: 33580431
Thanks Dave, your code works, but there isn't always a space at the end of the URL
0
 
LVL 2

Expert Comment

by:Maverickerko
ID: 33580438
my solution works for vary tt folder ... i
0
 
LVL 2

Author Comment

by:detox1978
ID: 33580489
Maverickerko, i dont see where my string fits into your snippet.
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 33580552
What else is there after the URL?  Here is a version that is much more specific for that URL.
<!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.";

$tchk = "http://www.imdb.com/title/tt";
$tchklen = strlen($tchk);
$tstart = strpos($teststr,$tchk);
$tend = strpos($teststr,"/",($tstart+$tchklen));

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

Open in new window

0
 
LVL 2

Author Closing Comment

by:detox1978
ID: 33580794
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);
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33580830
Thanks and that's cool.  I just never combine things that way because it makes my eyes cross to look at it.!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question