【问题标题】:get folder name from dynamic URL php从动态 URL php 获取文件夹名称
【发布时间】:2015-07-03 06:19:29
【问题描述】:

我的网址可能正在关注,

1) www.steptoinstall.com

2) www.steptoinstall.com/folder1

3)www.steptoinstall.com/folder1/index.php

4) www.steptoinstall.com/folder1/folder2

5)www.steptoinstall.com/folder1/folder2/

6) www.steptoinstall.com/folder1/folder2/index.php

7) www.steptoinstall.com/folder1/folder2/index.php?id=12

但我想要最后一个文件夹名称,比如

1) 没有

2) 文件夹1

3) 文件夹1

4) 文件夹2

5) 文件夹2

6) 文件夹2

7) 文件夹2

我用了以下,但没用。

<?php
  $url = $_SERVER['REQUEST_URI'];
  print_r(dirname($url));
?>

如何在 PHP 中而不是在 .htaccess 中获取此输出?

【问题讨论】:

    标签: php url uri


    【解决方案1】:
    $path = 'www.steptoinstall.com/folder1/folder2/';
    $folders = explode('/', $path);
    $i = 0;
    foreach($folders as $folder) {
        if (strpos($folder, '.') !== FALSE || empty($folder)) {
            unset($folders[$i]);
        }
        $i++;
    }
    $what_we_need = end($folders);
    echo $what_we_need;
    

    对不起,我的第一个答案很糟糕,这应该可以正常工作。

    【讨论】:

      【解决方案2】:

      也许这对你有用

      <?php
         $urls = array(
            'http://www.steptoinstall.com',
             'http://www.steptoinstall.com/folder1',
             'http://www.steptoinstall.com/folder1/folder2/index.php?id=12'
         );
      
      // The Function for your work
      function getFolder($url) {
          $res = parse_url($url);
          if(!isset($res['path'])) {
              return false;
          }
          $res = explode('/',$res['path']);
          $out = false;
          foreach($res as $p) {
              if(!empty($p) && strpos($p,'.')===false) $out = $p;
          }
          return $out;
      }
      
      // Testing
      foreach($urls as $url) {
         var_dump(getFolder($url));
      
      }
      

      【讨论】:

        【解决方案3】:

        如果我正确理解您的问题,您可以使用以下自定义函数

        function get_arg() {
           $arguments = array();
           $path = $_SERVER['REQUEST_URI'];
           if (isset($path)) {
              $arguments = explode('/', $path);
           }
           $url_len = count($arguments);
           return ($arguments[$url_len - 2]) ? $arguments[$url_len - 2] : 'none';
         }
        

        希望对你有帮助。

        【讨论】:

          【解决方案4】:

          我使用了正则表达式:

          <?php
          $data = array(
          'www.steptoinstall.com',
          'www.steptoinstall.com/folder1',
          'www.steptoinstall.com/folder1/index.php',
          'www.steptoinstall.com/folder1/folder2',
          'www.steptoinstall.com/folder1/folder2/',
          'www.steptoinstall.com/folder1/folder2/index.php',
          'www.steptoinstall.com/folder1/folder2/index.php?id=12',
          );
          
          foreach($data as $url)
          {
              print $url." ".get_folder($url)."\n";
          }
          
          function get_folder($url)
          {
              $url = preg_replace('/(.*)\/.*\..*/', '$1', $url);
              $url = preg_replace('/\/$/', '$1', $url);
          
              $matches = array();
              if (preg_match('/.*\/(.*)/', $url, $matches))
              {
                  return $matches[1];
              }
          
              return '';
          }
          

          输出:

          www.steptoinstall.com 
          www.steptoinstall.com/folder1 folder1
          www.steptoinstall.com/folder1 folder1
          www.steptoinstall.com/folder1/folder2 folder2
          www.steptoinstall.com/folder1/folder2 folder2
          www.steptoinstall.com/folder1/folder2 folder2
          www.steptoinstall.com/folder1/folder2 folder2
          

          您可能在这里遇到问题:很难区分“www.steptoinstall.com/folder1”和“www.steptoinstall.com/folder1/index.php” - 如果会有一个没有 .php 结尾的 SEF url .

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-03-19
            • 2014-03-17
            • 1970-01-01
            • 1970-01-01
            • 2015-02-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多