【问题标题】:How to remove path after domain name from string如何从字符串中删除域名后的路径
【发布时间】:2015-07-21 12:39:43
【问题描述】:

我有以下代码:

function removeFilename($url)
{
    $file_info = pathinfo($url);
    return isset($file_info['extension'])
        ? str_replace($file_info['filename'] . "." . $file_info['extension'], "", $url)
        : $url;
}

$url1 = "http://website.com/folder/filename.php";
$url2 = "http://website.com/folder/";
$url3 = "http://website.com/";
echo removeFilename($url1); //outputs http://website.com/folder/
echo removeFilename($url2);//outputs http://website.com/folder/
echo removeFilename($url3);//outputs http:///

现在我的问题是,当只有一个没有文件夹或文件名的域时,我的函数也会删除 website.com。

我的想法是 php 有什么方法可以告诉我的函数只有在第三个斜线或您认为有用的任何其他解决方案之后才能完成工作。

【问题讨论】:

  • 很遗憾,我没有得到任何想要的好答案。
  • 所有答案都很好,但并不完美
  • 检查我的答案,现在它满足您的所有需求

标签: php path


【解决方案1】:

更新:(工作和测试)

<?php
function removeFilename($url)
{
        $parse_file = parse_url($url);
        $file_info = pathinfo($parse_file['path']);
        return isset($file_info['extension'])
            ?  str_replace($file_info['filename'] . "." . $file_info['extension'], "", $url)
            :  $url;        
}

$url1 = "http://website.com/folder/filename.com";
$url2 = "http://website.org/folder/";
$url3 = "http://website.com/";

echo removeFilename($url1); echo '<br/>';
echo removeFilename($url2); echo '<br/>';
echo removeFilename($url3);
?>

输出:

http://website.com/folder/
http://website.org/folder/
http://website.com/

【讨论】:

  • 如果是 windows com 文件?
  • 如果 url 是 .org 或 .net?
  • @mus ,它也适用于所有人(.org、.com、.net 等)
  • 是的,现在这是一个完美的答案;)你更新了它(y),因为首先你有 $file_info['extension'] != 'com'
  • 好的,这是公认的答案,感谢您的帮助,祝您有美好的一天! ♥
【解决方案2】:

听起来您想要替换子字符串而不是整个内容。此功能可能对您有所帮助:

http://php.net/manual/en/function.substr-replace.php

【讨论】:

    【解决方案3】:

    由于文件名最后是斜杠,您可以使用substr 和str_replace 从路径中删除文件名。

    $PATH = "http://website.com/folder/filename.php";
    
    $file = substr( strrchr( $PATH, "/" ), 1) ; 
    echo $dir = str_replace( $file, '', $PATH ) ;
    

    输出

    http://website.com/folder/

    【讨论】:

    • 你能解释一下它为什么不工作吗???你得到什么输出或错误???还解释一下你是如何测试这段代码的???
    • 不给http://website.com/检查它的输出$PATH = "http://website.com/"; $file = substr( strrchr( $PATH, "/" ), 1) ; echo $dir = str_replace( $file, '', $PATH ) ;
    • website.com / 这将输出 website.com / 但website.com 这将输出 http://
    【解决方案4】:

    pathinfo 不能只识别域和文件名。但是如果没有文件名 url 以斜杠结束

    $a = array(
    "http://website.com/folder/filename.php",
    "http://website.com/folder/",
    "http://website.com",
    );
    
    foreach ($a as $item) {
       $item = explode('/', $item);
       if (count($item) > 3)
          $item[count($item)-1] ='';;
       echo implode('/', $item) . "\n";
    
    }
      result
    
    http://website.com/folder/
    http://website.com/folder/
    http://website.com
    

    【讨论】:

    • 您的代码尝试删除路径的最后一个文件夹
    • 如果 url 像这样website.com/filename.php,它不能删除文件名
    • 是的。我在答案的开头写了这个
    【解决方案5】:

    接近splash58的答案

    function getPath($url) {
        $item = explode('/', $url);
        if (count($item) > 3) {
            if (strpos($item[count($item) - 1], ".") === false) {
                return $url;
            }
            $item[count($item)-1] ='';
            return implode('/', $item);
        }
        return $url;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-22
      • 2010-09-12
      • 1970-01-01
      • 2019-03-04
      • 2023-04-02
      • 2011-11-28
      相关资源
      最近更新 更多