Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php打印删除部分url_Php - Fatal编程技术网

使用php打印删除部分url

使用php打印删除部分url,php,Php,我想退休网页网址使用php,但我想删除一些部分 <?php print("http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); ?> Example: http://url.com/questions/page/112/ Result: http://url.com/page/112/ 例子:http://url.com/questions/page/112/ 结果:http://url.com/page/1

我想退休网页网址使用php,但我想删除一些部分

<?php print("http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); ?>


Example: http://url.com/questions/page/112/
Result: http://url.com/page/112/

例子:http://url.com/questions/page/112/
结果:http://url.com/page/112/

我想删除url中的
问题/
。我将如何做到这一点?

我将使用php的explode函数将示例拆分为一个以“/”分隔的数组,然后循环遍历该数组,其中数组值=questions,取消设置或将其从数组中移除

//例1

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

这里有一个例子

如果只想将其作为字符串删除,可以使用

$url = str_replace('/questions', '', $_SERVER["REQUEST_URI"]);
如果然后要将用户重定向到该页面,则需要发送标题(在任何输出之前):

试试这个

$str = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];

$parts = explode("/",$str);

$tmp = array();
for($i = 0; $i<count($parts)-2;$i++){
   $tmp[$i] = $parts[$i];
}

$output = implode("/",$tmp);
$str=“http://”$_服务器[“HTTP_主机”]$_服务器[“请求URI”];
$parts=explode(“/”,$str);
$tmp=array();

对于($i=0;$i您可以使用类似的内容

$sentence = str_replace('questions/', '', 'http://url.com/questions/page/112/');

您需要使用mod_rewrite,这是apache中提供的一个模块。它将由web目录中的.htaccess文件管理。AddedBytes为初学者提供了一个很好的url重写教程

$sentence = str_replace('questions/', '', 'http://url.com/questions/page/112/');
//This splits the uri into an array
$uri = explode("/",$_SERVER["REQUEST_URI"]);

//Then Remove the first part of the uri (ie questions)
$first_uri = array_shift($uri);

//Recreate the string from the array
$uri = implode("/", $uri);

//Print like in your example
print("http://" . $_SERVER["HTTP_HOST"] . $uri);

//You can also access the remove string (questions) in the $first_uri variable
print($first_uri); //returns questions
$url="http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$url=str_replace('/questions','',$url);
echo $url;
$url='http://'.$_SERVER['HTTP_HOST'].preg_replace('/^\/questions/i','',$_SERVER['REQUEST_URI']);
echo $url;