Fetching a specific part of a string in PHP

I'm just wondering what would be the easiest and most efficient way to extract a specific part of a dynamic string in PHP?

For example, in this line:

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

I would just like to extract (and insert into the variable): "xclep1_school-gyrls-something-like-a-party_music".

The main goal is to accept this part, paste it into this URL: http://www.dailymotion.com/thumbnail/160x120/video/xclep1_school-gyrls-something-like-a-party_music so that I can record the thumbnail from the outside.

Sorry if this is a “newbies” question and thank you very much for your time. Any hint / code / php links are appreciated.

+3
source share
4 answers

Try parse_urlon regex:

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

$ segments will be an array containing all segments of path information, for example.

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`
+12
source

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, '/'));
+3
source

Parse_url () function and extract the path. Detonate at '/' and get the last item

+2
source

try it

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

Source: https://habr.com/ru/post/1751722/


All Articles