如何获取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];
<?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];
}
?>
$str
的原始输出是什么? - Kevin