从php中的url中提取文件夹名称

时间:2015-05-08 18:09:10

标签: php


我遇到了一个麻烦,我怎么能从网址中提取文件夹名称 示例 - http://localhost/posts/author/FOLDER_NAME/live/?red=home
我想回复FOLDER_NAME到我的网页

<?php echo $name; ?>

2 个答案:

答案 0 :(得分:1)

您可以按“/”分解字符串并获取正确的元素:

print explode("/", "http://localhost/posts/author/FOLDER_NAME/live/?red=home")[5];

答案 1 :(得分:1)

有几种方法可以满足您的需求,一种简单的方法就是使用preg_replace

$url = "http://localhost/posts/author/FOLDER_NAME/live/?red=home"
$folder = preg_replace('%.*author/(.*?)/live.*%', '$1', $url);
echo $folder ;

输出:

FOLDER_NAME

根据您的评论更新:

$url = "http://localhost/posts/author/FOLDER_NAME?red=home"; 
$folder = preg_replace('%.*author/(.*?)\?.*%', '$1', $url);
echo $folder ;

输出:

FOLDER_NAME