Get string between slashes of a PHP variable

-3

How to get the string that is inside the first two bars (naacoclqtsafkbsk) with PHP?

I get the url through GET

$url = $_GET['url']

// https://streamango.com/embed/naacoclqtsafkbsk/L3j3md0fD3N4g4P34rIz_Legendado_mp4
    
asked by anonymous 16.12.2018 / 23:21

2 answers

1

The value you want is what we call the URL path segment ( segment path ). For this, the ideal is to treat the URL as a URL through the parse_url function:

$url = parse_url('https://streamango.com/embed/naacoclqtsafkbsk/L3j3md0fD3N4g4P34rIz_Legendado_mp4');

So, we just took the path part of the URL:

$path = trim($url['path'] ?? '/', '/');

With the ?? operator, we ensure that if the URL does not have a path the string "/" is considered, as recommended by the RFC. With the function trim we remove any bar from the beginning or end of the path , because for us at this point, they will not be useful.

For the given string in question, the value of $path will be:

embed/naacoclqtsafkbsk/L3j3md0fD3N4g4P34rIz_Legendado_mp4

So, we split the string into the slashes, generating an array with all URL path segments:

$segments = explode('/', $path);

What will be the array:

Array
(
    [0] => embed
    [1] => naacoclqtsafkbsk
    [2] => L3j3md0fD3N4g4P34rIz_Legendado_mp4
)

With this, you only have to define which position of the array is interesting to you, since comment This does not seem to be very clear yet.

    
17.12.2018 / 00:22
1

Take a look at parse_url () - Performs a URL and returns its components .

The part you are interested in is the path, so you can pass PHP_URL_PATH as the second argument.

$url = "https://streamango.com/embed/naacoclqtsafkbsk/L3j3md0fD3N4g4P34rIz_Legendado_mp4";

//var_dump(parse_url($url, PHP_URL_PATH));
//string(58) "/embed/naacoclqtsafkbsk/L3j3md0fD3N4g4P34rIz_Legendado_mp4"

$uriSegmentos = explode("/", parse_url($url, PHP_URL_PATH));

echo $uriSegmentos[2];

ideone

    
17.12.2018 / 01:37