user3375691
user3375691

Reputation: 113

Getting A Certain Part Of A Url

How can i get a certain part of a URL i have searched google and read a few tutorials but can't seem to get my head around it maybe someone can show me from the example below.

Here is my code

<?php

include "simple_html_dom.php";

$title = "fast";

$html = file_get_html("http://www.imdb.com/find?q=".urlencode($title)."&s=all");
$element = $html->find('[class="result_text"] a', 1);
$link = $element->href;
echo $link;

// Clear dom object
$html->clear(); 
unset($html);

?>

Now this echos

/title/tt0109772/?ref_=fn_al_tt_2

But i only want the imdb id.

tt0109772

So can someone explain or show me how to do this please

thanks

Upvotes: 0

Views: 94

Answers (5)

Awlad Liton
Awlad Liton

Reputation: 9351

you can Use explode():

$str = "/title/tt0109772/?ref_=fn_al_tt_2"; 
$arrUrl = explode("/",$str);
$id = $arrUrl[2];
echo $id;

demo

Upvotes: 1

magnetronnie
magnetronnie

Reputation: 505

Try this code: (source)

$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', $path);

Then I think you need $segments[1] to get the IMDB id.

Upvotes: 0

emsoff
emsoff

Reputation: 1605

Not the most elegant way, but

$link = '/title/tt0109772/?ref_=fn_al_tt_2';
$imdb_id = explode('/', $link);
$imdb_id = $imdb_id[2];
echo $imdb_id;

Will work.

Upvotes: 0

Hardy
Hardy

Reputation: 5631

You can use explode like:

$parts = explode("/", $url);

This will split URL to array. Then check what index you want.. and get it like:

$id = $parts[3];

You can debug with: print_r($parts);

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 78994

If it's always the subdir:

echo basename(dirname($link));

Upvotes: 1

Related Questions