Question

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

Was it helpful?

Solution

If it's always the subdir:

echo basename(dirname($link));

OTHER TIPS

you can Use explode():

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

demo

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);

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top