2016-03-07 22 views
2
$pathInfo = $_SERVER['REDIRECT_URL']; 

forwordslash(/)結束。這是我的代碼,它會返回字符串刪除第一個單詞:如何從PHP中

/google/agency/create 

不過,我只需要agency/create/google是動態的詞。

+1

你可以用'/'分割字符串並獲取數組並使用數組元素獲取單詞 – Deep

+1

使用preg_replace()函數在php –

+1

你的問題是非常籠統的,並不清楚你想實現什麼。這是代碼將返回的唯一結果嗎?如果可以有更多的可能性,請舉例說明。 –

回答

1

你可以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); 

?> 

輸出:

機構/創建

Working Example

+0

感謝Samir的回覆... –

+0

@RahulPawar,我相信Samir的解決方案是完美的。如果你能接受它會有一些幫助。幫助那些幫助你的人。 –

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

你會得到你答agency/create

0
$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.