2013-10-23 13 views
7

ho il mio url:URL - Get ultima parte in PHP

http://domain/fotografo/admin/gallery_bg.php 

e voglio ultima parte dell'URL:

gallery_bg.php 

ma, io non voglio collegare statico, vale a dire, per ogni pagina che vistitar voglio ottenere l'ultima parte dell'URL

+0

Si prega di mostrare il vostro approccio –

+0

quello che hai provato finora? –

risposta

16

uso seguente

<?php 
    $link = $_SERVER['PHP_SELF']; 
    $link_array = explode('/',$link); 
    echo $page = end($link_array); 
?> 
4
$url = "http://domain/fotografo/admin/gallery_bg.php"; 
$keys = parse_url($url); // parse the url 
$path = explode("/", $keys['path']); // splitting the path 
$last = end($path); // get the value of the last element 
10

funzione Use basename

echo basename("http://domain/fotografo/admin/gallery_bg.php"); 
0
$url = $_SERVER["PHP_SELF"]; 
$path = explode("/", $url); 
$last = end($path); 
4

Se è stessa pagina:

echo $_SERVER["REQUEST_URI"]; 

or 

echo $_SERVER["SCRIPT_NAME"]; 

or 

echo $_SERVER["PHP_SELF"]; 

In ogni caso apparirà una barra posteriore (/ gallery_bg.php). È possibile tagliare come

echo trim($_SERVER["REQUEST_URI"],"/"); 

o dividere l'url da / per fare un array e ottenere l'ultimo elemento da serie

$array = explode("/",$url); 

$last_item_index = count($url) - 1; 

echo $array[$last_item_index]; 

o

echo basename($url); 
1

Prova questa:

Here you have 2 options. 

1. Using explode function. 

$filename = end(explode('/', 'http://domain/fotografo/admin/gallery_bg.php')); 

2. Use basename function. 

$filename = basename("http://domain/fotografo/admin/gallery_bg.php"); 

- Grazie

0

è possibile utilizzare la funzione basename ($ url) come suggerito sopra. Questo restituisce il nome del file dall'URL. È inoltre possibile fornire l'estensione del file come secondo argomento a questa funzione come basename ($ url, '.jpg'), quindi verrà fornito il nome del file senza l'estensione.

Esempio:

$url = "https://i0.com/images/test.jpg" 
 

 
then echo basename($url) will print test.jpg 
 

 
and echo basename($url,".jpg") will print test

0
$basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/'; 
    $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath)); 
    if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?')); 
    $url = trim($uri, '/'); 

In PHP 7 la soluzione accettata mi sta dando l'errore che solo le variabili sono ammessi in esplodere così questo funziona per me.