如何获取url中第二个斜杠后的字符串?URL每次都不一样(斜杠更多),但每次我都需要第二个斜杠之后的整个文本。怎么做?
我使用的是这个代码:
<?php
$str = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$last = substr($str, strrpos($str, '/') - 1);
echo $last;
?>
但它可以在线使用斜杠后的一些字符。
非常感谢你的帮助。
$last = explode("/", $str, 3);
echo $last[2];
使用
<?php
$str = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$last = explode("/",$str,3);// so at second index rest of the string will come.
echo $last[2];
看这里http://www.w3schools.com/php/func_string_explode.asp
<?php
$str = "google.com/whatever/hello";
$last = GetStringAfterSecondSlashInURL($str);
echo $last;
function GetStringAfterSecondSlashInURL($the_url)
{
$parts = explode("/",$the_url,3);
if(isset($parts[2]))
return $parts[2];
}
?>