Solved

PHP: find URL in a string

Posted on 2010-09-01
11
609 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 30

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 82

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
 
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 82

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 82

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 82

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now