removing part of a string with php

3

I have a variable with the following link:

https://www.youtube.com/embed/z1n34sRv1-A

I need to remove everything that comes before the embed, or I need the following id:

z1n34sRv1-A

How can I do this with php?

    
asked by anonymous 12.08.2016 / 20:34

4 answers

2

You can use the explode to divide the string by the bars and get the last position that is the count of the array:

$url_parts = explode("/", "https://www.youtube.com/embed/z1n34sRv1-A");
echo $url_parts[count($url_parts)-1];
    
12.08.2016 / 20:42
3

You can do it this way:

$url = "https://www.youtube.com/embed/z1n34sRv1-A";
$url = explode("embed/", $url);
$embed = $url[1];
    
12.08.2016 / 20:40
1

use

 substr($string,$start,$length);

$ string = variable, $ start = start of the variable where var cut the string into integer, $ length = end of integer variable,

let's say that the url pattern has 25 characters would be

substr($url,25,strlen($url));
    
12.08.2016 / 20:42
1

Use strrpos to detect the last / of url and, with substr , extract the desired part:

$url = 'https://www.youtube.com/embed/z1n34sRv1-A';

substr($url, strrpos($url, '/') + 1)

The strrpos function finds the last occurrence of / and returns the position. Since we need to get what comes after that, we need to add +1.

Still using a line, you can make it easier to use array_reverse to get only the last element of array generated by explode .

 current(array_reverse(explode('/', $url)))
    
12.08.2016 / 20:50