“无效的“..”-范围,“..”左侧没有字符”调用trim()时出错

发布于 2025-01-07 11:49:13 字数 280 浏览 0 评论 0原文

假设我有 $url="../folder/file" 并且我想查找并删除 ../ 部分。

我正在使用 trim() ……

$url = trim($url,"../");

但它给了我一个警告:

警告:trim() [function.trim]:无效的“..”范围,上一行“..”左侧没有字符

我做错了什么?

Let's say I have $url="../folder/file" and I want to find and remove the ../ part.

I'm using trim()

$url = trim($url,"../");

… but it gives me a warning:

Warning: trim() [function.trim]: Invalid '..'-range, no character to the left of '..' on line above

What I did wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

§对你不离不弃 2025-01-14 11:49:13

你做错的是没有阅读手册:

使用 .. 您可以指定字符范围。

<?php
$url="../folder/file";
$url = trim($url,"\.\./");
echo $url;
?>

what you did wrong was fail to read the manual:

With .. you can specify a range of characters.

<?php
$url="../folder/file";
$url = trim($url,"\.\./");
echo $url;
?>
烟火散人牵绊 2025-01-14 11:49:13

你可以使用 ltrim

echo ltrim("../folder/file", "./");

echo trim("../folder/file", "./");

you can use ltrim

echo ltrim("../folder/file", "./");

or

echo trim("../folder/file", "./");
黒涩兲箜 2025-01-14 11:49:13

php.net/trim 的修剪函数中有一个特殊的语法,允许您指定一个范围,即解释器认为您正在做什么,因为“..”

// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
$clean = trim($binary, "\x00..\x1F");
var_dump($clean);

修剪的第二个参数应该是一个将被删除的字符串,因此您不必放置“.”。两次。

There is a special syntax in the trim function from php.net/trim that allows you to specify a range, which is what the interpreter believes you are doing because of the '..'

// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
$clean = trim($binary, "\x00..\x1F");
var_dump($clean);

The second argument to trim should be a string of characters that would be stripped, so you should not have to put the '.' twice.

无声无音无过去 2025-01-14 11:49:13

修剪函数的第二个参数指定要删除的字符列表,而不是多字符字符串。该函数将“..”解释为指定字符范围的运算符(如 a..z 或 1..5)。您可以通过多种方式去掉“../”,但一个简单的方法是:

$parts = explode('/', $url);
array_shift($parts);
$url = implode('/', $parts);

The second argument to the trim function specifies a list of characters to be stripped, not a multi-character string. The function interprets '..' as an operator specifying a range of characters (like a..z or 1..5). You can strip out the '../' in a number of ways, but one easy one is this:

$parts = explode('/', $url);
array_shift($parts);
$url = implode('/', $parts);
绝不放开 2025-01-14 11:49:13

我在 php.net 上的修剪页面的底部评论中找到了这个函数,它似乎可以做你想要的

function trimString($input, $string){
        $input = trim($input);
        $startPattern = "/^($string)+/i";
        $endPattern = "/($string)+$/i";
        return trim(preg_replace($endPattern, '', preg_replace($startPattern,'',$input)));
} 

I found this function in bottom comments of the trim page on php.net that seem to do what you want

function trimString($input, $string){
        $input = trim($input);
        $startPattern = "/^($string)+/i";
        $endPattern = "/($string)+$/i";
        return trim(preg_replace($endPattern, '', preg_replace($startPattern,'',$input)));
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文