【问题标题】:How to get first 3 parts of URL in PHP?如何在 PHP 中获取 URL 的前 3 部分?
【发布时间】:2014-04-25 05:19:19
【问题描述】:

如何使用 PHP 获取当前 URL 的前 3 部分。

例如:

我的网址:http://something.com/somebody/somegirls/whatever/

得到零件后的结果:http://something.com/somebody/somegirls/

这是我获取当前 URL 的 PHP 代码:

<?php   function curPageURL() {
        $url  = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http';
        $url .= '://' . $_SERVER['SERVER_NAME'];
        $url .= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT'];
        $url .= $_SERVER['REQUEST_URI'];
        return $url;
    }

 $current_url = str_replace("www.", "", curPageURL());

?>

【问题讨论】:

标签: php url


【解决方案1】:

试试这个,

<?php
  $url = 'http://something.com/somebody/somegirls/whatever/';
  $parts = explode('/', $url);
  $new_url = $parts[0].'/'.$parts[1].'/'.$parts[2].'/'.$parts[3].'/'.$parts[4].'/';
  echo $new_url;
?>

输出

http://something.com/somebody/somegirls/

【讨论】:

    【解决方案2】:

    假设你已经从你的函数中获取了这个 URL...

    <?php
    $url='http://www.something.com/somebody/somegirls/whatever/';
    $parts=explode('/',parse_url($url)['path']);
    array_unshift($parts,trim(strstr(parse_url($url)['host'],'.'),'.'));
    print_r(array_filter($parts));
    

    OUTPUT :

    Array
    (
        [0] => something.com
        [2] => somebody
        [3] => somegirls
        [4] => whatever
    )
    

    Demonstration

    【讨论】:

    • 修改了答案,使其也适用于www
    【解决方案3】:

    您也可以使用parse_url 来获取数组中的部分网址,如下所示:

    $current_url_array = parse_url($current_url);
    var_dump($current_url_array);
    

    【讨论】:

      【解决方案4】:

      你可以使用正则表达式:

      <?php
      
          function getFirstUrlContents($Url) {
              preg_match_all('/^([^\/]*\/){5}/', $Url, $MatchesArray);
              return $MatchesArray[0];
          }
      
          var_dump(getFirstUrlContents('http://something.com/somebody/somegirls/whatever/'));
      
      ?>
      

      【讨论】:

        【解决方案5】:
        <?php
           $url='http://something.com/somebody/somegirls/whatever' ;
            $explode=explode("/", $url);
            $search=end($explode);
        
           echo $currentUrl=str_replace($search,'',$url);
        ?>
        

        输出

        http://something.com/somebody/somegirls/
        

        【讨论】:

          【解决方案6】:

          试试这个:

          <?php
          $url = 'http://something.com/somebody/somegirls/whatever/';
          $pos = explode('/', $url);
          for($i=0; $i<5; $i++){
              echo $pos[$i].'/';
          }
          ?>
          

          输出:http://something.com/somebody/somegirls/

          【讨论】:

            【解决方案7】:

            请检查以下代码。

            function createUrl($array, $pos) {
                $string = '';
                for ($i = 0; $i < $pos; $i++)
                $string .=$array[$i].'/';
                return $string;
            }
            
            $current_url = "http://something.com/somebody/somegirls/xyz/yui";
            $initial_string = (stripos($current_url, 'https://') !== FALSE) 
                                   ? 'https://'
                                   : ((strpos($a, 'http://') !== FALSE)
                                   ? 'http://' : '');
            $last_string = explode('/', substr($a, strlen($initial_string)));
            
            $final_url = $initial_string.
                    (count($last_string) > 3)
                    ? createUrl($last_string, 3)
                    : substr($current_url, strlen($initial_string));
            echo $final_url;
            

            【讨论】:

              猜你喜欢
              • 2011-10-22
              • 1970-01-01
              • 1970-01-01
              • 2013-06-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-01-26
              相关资源
              最近更新 更多