tags:

views:

85

answers:

4

Hey guys,

I was simply wondering what would be the simplest and most efficient way of extracting a certain part of a dynamic string in PHP?

Per example, in this string:

http://www.dailymotion.com/video/xclep1_school-gyrls-something-like-a-party_music#hp-v-v13

I'd only like to extract (and insert in a variable) the: " xclep1_school-gyrls-something-like-a-party_music ".

The main goal is to take this part, insert it to this URL: http://www.dailymotion.com/thumbnail/160x120/video/xclep1_school-gyrls-something-like-a-party_music so I can capture the thumbnail externally.

Sorry if this is a "newbie" question and thank you very much for your time. Any hint/code/php reference is appreciated.

+2  A: 

One of the following:

preg_match('~/[^/]*$~', $str, $matches);
echo $matches[0];

Or:

$parts = explode('/', $str);
echo array_pop($parts);

Or:

echo substr($str, strrpos($str, '/'));
Sjoerd
Thanks a million! I actually tried several times with a preg_match, but sadly I'm not a regex-guru. I will try each of those and see which works best. Thank you again.
Did you forget the end part of the string `#hp-v-v13`?
SpawnCxy
+2  A: 

The parse_url() function and extract the path. Explode on '/' and get the last element

Mark Baker
+9  A: 

Try parse_url over regex:

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

$segments will be an array containing all segments of the path info, e.g.

Array
(
    [0] => 
    [1] => video
    [2] => xclep1_school-gyrls-something-like-a-party_music
)

So you can do

echo $segments[2];

and get

`xclep1_school-gyrls-something-like-a-party_music`
Gordon
As expected, it works perfectly. Thank you very(!) much for your kind help.Now if I could just figure out why my question got downvoted...
@justin01 wasn't me (i just upvoted it) and you're welcome
Gordon
+1  A: 

Try this

$url = $_SERVER['REQUEST_URI'];
$parsed_url = parse_url($url);
$url_parts = explode('/',$parsed_url['path']);
print_r($url_parts);
bharath