开发者

trim slashes from Request_Uri

here's my code snippet to start with:

$url = $_SERVER["REQUEST开发者_StackOverflow社区_URI"]; // gives /test/test/ from http://example.org/test/test/
echo"$url"; 
trim ( $url ,'/' );
echo"$url";

I use this in combination with .htaccess rewrite, I’ll get the information from the URL and generate the page for the user with PHP using explode. I don't want .htaccess to interpret the URL, which is probably better, but I am more common with PHP and I think it’s more flexible.

I already read this (which basically is what I want): Best way to remove trailing slashes in URLs with PHP

The only problem is, that trim doesn’t trim the leading slashes. Why? But actually it should work. Replacing '/' with "/", '\47' or '\x2F' doesn’t change anything. It neither works online nor on localhost. What am I doing wrong?


The trim function returns the trimmed string. It doesn't modify the original. Your third line should be:

$url = trim($url, '/');


This can be done in one line...

echo trim($_SERVER['REQUEST_URI'], '/');


You need to do:

$url = trim($url, '/');

You also should just do

echo $url;

It is faster.


trim does not modify the original. You'll need to do something such as:

$url = $_SERVER["REQUEST_URI"]; // gives /test/test/ from http://example.org/test/test/
echo"$url"; 
$url = trim ( $url ,'/' );
echo"$url";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜