gpt4 book ai didi

php - 如何在 PHP 中获取 URL 的前 3 部分?

转载 作者:可可西里 更新时间:2023-11-01 13:06:53 25 4
gpt4 key购买 nike

如何使用 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 = '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/

关于php - 如何在 PHP 中获取 URL 的前 3 部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23284713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com