O
O
Outoverlay2016-02-08 22:50:44
PHP
Outoverlay, 2016-02-08 22:50:44

How to get the latest?

There is a url address with unlimited number of levels and how to always get the last level. Example page-2 / sub-page-2 / how to get from it - "sub-page-2", and if let's say there is sub-page-3, then display it?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
DevMan, 2016-02-08
@Outoverlay

echo basename($url), PHP_EOL;
// если адрес содержит get-параметры или якоря
echo basename(parse_url($url)['path']), PHP_EOL;
ideone.com/fMIXvt
is possible and regular, but why?

E
entermix, 2016-02-08
@entermix

function get_last($url){
    $result = explode('/', trim(parse_url($url, PHP_URL_PATH), '/'));
    return end($result)
}

print get_last('страница-2/под-страница-2/'); // под-страница-2

X
xmoonlight, 2016-02-08
@xmoonlight

$p=explode('?',$url);
$p=explode('/',$p[0]);
if($p[count($p)-1]!='') $p=$p[count($p)-1];
else $p=$p[count($p)-2];
echo $p;

M
Maxim E, 2016-02-08
@creativeworm

If it's a crutch, then you can do this:

<?php
$url = "страница-2/под-страница-2/еще/еще-77-раз/";
$temp_result = explode("/", $url); // Array ( [0] => страница-2 [1] => под-страница-2 [2] => еще [3] => еще-77-раз [4] => )
$temp_result = array_diff($temp_result, array('')); // удаляем пустые элементы
echo end($temp_result); // выводим моследний элемент
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question