如何获取网址的特定部分?

时间:2014-06-04 06:45:45

标签: php

我正在研究搜索功能,我想让我的搜索更容易,并且我在我的数据库中存储了href。因此,我需要获取当前页面网址的特定部分,例如: abc.php。 但现在我只能获得的完整网址,例如: http://abc_system/user/abc.php。是否使用substring解决方案之一?我正在寻求一些帮助。希望你们能帮助我。谢谢你提前。

这是我的代码,它返回url结果:

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

2 个答案:

答案 0 :(得分:1)

您需要使用basename()函数从网址字符串中获取filename

<?php
   $url = "http://google.com/sdfsaf/abcd.php";
   echo basename($url);  // It will returns abcd.php
?>

<强> Demo

答案 1 :(得分:0)

如果要将网址拆分为其组件,请使用**parse_url()**, 有关详细信息,请参阅:http://www.php.net/manual/en/function.parse-url.php

相关问题