2017-09-28 13 views

Répondre

1

vous pouvez utiliser et vous pouvez obtenir votre sortie requise:

// implode string into array 
$url = "http://192.168.0.16/wordpress/blog/page-2/"; 
//then remove character from right 
$url = rtrim($url, '/'); 
// then explode 
$url = explode('/', $url); 
// remove the last element and return an array 
json_encode(array_pop($url)); 
// implode again into string 
echo implode('/', $url); 

Une autre approche est la suivante:

// implode string into array 
$url = explode('/', 'http://192.168.0.16/wordpress/blog/page-2/'); 
//The array_filter() function filters the values of an array using a callback function. 
$url = array_filter($url); 
// remove the last element and return an array 
array_pop($url); 
// implode again into string 
echo implode('/', $url); 
+0

sortie: http://192.168.0.16/wordpress/blog/page-2 –

+0

supprimer la dernière barre oblique de url.it va fonctionner –

+0

vous pouvez ajouter ce $ url = array_filter ($ url); –

0
$url = 'http://192.168.0.16/wordpress/blog/page-2/'; 
    // trim any slashes at the end 
    $trim_url = rtrim($url,'/'); 
    // explode with slash 
    $url_array = explode('/', $trim_url); 
    // remove last element 
    array_pop($url_array); 
    // implade with slash 
    echo $new_url = implode('/', $url_array); 

Sortie:

http://192.168.0.16/wordpress/blog 
0

La manière correcte serait être à utiliser parse_url() et dirname(), qui prendra également en charge les paramètres de requête. Vous pourriez exploser $uri['path'] mais c'est inutile dans ce cas.

<?php 
// explode the uri in its proper parts 
$uri = parse_url('/wordpress/blog/page-2/?id=bla'); 

// remove last element 
$path = dirname($uri['path']); 

// incase you got query params, append them 
if (!empty($uri['query'])) { 
    $path .= '?'.$uri['query']; 
} 

// string(22) "/wordpress/blog?id=bla" 
var_dump($path); 

voir travailler:https://3v4l.org/joJrF