Cut string from the second "/" - PHP

0

I have a route saved in a variable:

$path = raiz/email_usuario/Imagenes/Wallpaper/../.......

I need to remove $ path "raiz/email_usuario" , I can not use explode since the email can be bigger or smaller, I thought about picking up from the second "/" but I would not know how to do it. The final result should be "Imagenes/Wallpaper...."

Thank you!

    
asked by Fabián Moreno 17.05.2018 в 22:45
source

3 answers

1

I leave you this function that was shared in the contributions of users of the PHP Manual , in the part relative to strtok .

I think it comes as a glove for what you need:

function subtok($string,$chr,$pos,$len = NULL) {
    return implode($chr,array_slice(explode($chr,$string),$pos,$len));
}

To use it in this particular case, it would be like this:

echo  subtok($path,'/',2) ;

That is, you pass the route to it, tell it to use the / separator and extract the information from the second separator.

The result would be:

Imagenes/Wallpaper/.../...

The advantage is that if you need other types of extractions, by another separator, or change the position you want to extract, you only have to change the parameters that you pass to the function.

For example:

echo  subtok($path,'/',1) ;

Would give you as a result:

email_usuario/Imagenes/Wallpaper/.../...

Here you have a DEMO complete, and more details and examples of the function. I hope it serves you.

    
answered by 18.05.2018 в 03:35
0

This may work for you:

$path = 'raiz/email_usuario/test/test2/';
$explode = explode('/',$path);

var_dump($var[0].'/'.$var[1]);
    
answered by 17.05.2018 в 23:00
0

One more option

<?php
$path = "raiz/email_usuario/Imagenes/Wallpaper/../.......";
$imagenesPath = implode('/', array_slice(explode('/', $path), 2));

print_r($imagenesPath);
    
answered by 18.05.2018 в 00:47