Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP: find URL in a string

Posted on 2010-09-01
11
Medium Priority
?
659 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 84

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 84

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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 84

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

618 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