如何在 php 中删除以 forwordslash(/) 结尾的字符串中的第一个单词


How to remove first word from string that ends with forwordslash(/) in php

$pathInfo = $_SERVER['REDIRECT_URL'];

这是我的代码,它将返回:

/google/agency/create

但是,我只需要agency/create /google因为这是动态词。

您可以explode URL字符串,unset第一个值,然后再次implode

<?php
$url = "/google/agency/create";
$exp = explode("/", trim($url, "/")); // trim the '/' from the URL and explode it
unset($exp[0]); // unset first value
$final_arr = implode("/", $exp); // form the string again
print_r($final_arr);
?>

输出:

代理/创建

工作示例

$var = "/google/agency/create";
$ex = explode('/',$var);
unset($ex[0]); unset($ex[1]);
echo implode("/",$ex);

你会得到你的安斯agency/create

$pathInfo = $_SERVER['REDIRECT_URL']; //   /google/agency/create
$array = spliti("/", $pathInfo, 3); //Array([0] =>[1] => google[2] => agency/create)
echo $array[2]; // required output : agency/create.