I
I
Ivan Fotiev2015-10-15 23:49:46
PHP
Ivan Fotiev, 2015-10-15 23:49:46

What is the best way to trim the contents of a variable?

Hello. What is the best way to bring the variable to the desired form? The variable $a="/store/bokal/skatert-velvet9/" needs to be cut to the form $b="/store/bokal/
I have an idea to do this:

$a = "/store/bokal/skatert-velvet9/";
list($one $two,three) = explode("/", $a);
$url = "/"."$two"."/"."$three"."/";
echo $url;

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
maddog670, 2015-10-16
@ifotev

Could it be something like this

$a = "/store/bokal/skatert-velvet9/";
$exp = explode("/", $a);
$url = "/{$exp[1]}/{$exp[2]}/";
echo $url;

M
Michael, 2015-10-15
@scherbuk

$url = "/$two/$three/";

A
Alexey, 2015-10-16
@dzheka3d

$a="/store/bokal/skatert-velvet9/";
$a= str_replace('/store', '', $a);

F
FMars, 2015-10-16
@FMars

$a = "/store/bokal/skatert-velvet9/";
$l = explode("/", trim($a, '/'));
$url = "/$l[0]/$l[1]/";

M
Muhammad, 2015-10-16
@muhammad_97

$segments = explode('/', rtrim($url, '/'));
// удаляем последний элемент
array_pop($segments);
// для слеша в конце
$segments[] = '';
$url = implode('/', $segments);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question