Solved

PHP: find URL in a string

Posted on 2010-09-01
11
634 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
  • 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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.

810 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