简体   繁体   English

清理文件路径字符串,最后只允许 1 个尾部斜杠

[英]Sanitize filepath string and only allow 1 trailing slash at the end

I need to remove non alphanumeric characters except _ (underscore) and - dash and only one / (forward slash) from the end of a string.我需要从字符串末尾删除 _(下划线)和 - 破折号以及一个 /(正斜杠)以外的非字母数字字符。

$string = 'controller_123/method///';

or或者

$string = 'controller_123/method/';

Both should return: 'controller_123/method/' ;两者都应返回: 'controller_123/method/'

What I have tried up to now:到目前为止我已经尝试过:

$string = preg_replace('/[^a-zA-Z0-9_]\/$/', '', $string);

You can use preg_replace with arrays of patterns and replacements; 您可以将preg_replace与模式和替换数组一起使用; the first to remove non-alphanumeric characters other than _ , - and / , and the second to remove all but the last trailing / : 第一个删除_-/以外的非字母数字字符,第二个删除除最后一个尾号/所有字母:

$string = 'controller_123/method///';
echo preg_replace(array('#[^\w/-]+#', '#/+$#'), array('', '/'), $string);

Output: 输出:

controller_123/method/

Demo on 3v4l.org 3v4l.org上的演示

The regex can be improved by noting that we want to remove all / before the one at the end of the line, and using a positive lookahead to match those. 注意,我们要删除行末尾的所有/ ,并使用正向前行匹配,以改进正则表达式。 Then all matches can simply be replaced with an empty string: 然后,所有匹配项都可以简单地替换为空字符串:

$string = 'contr*@&oller_123////method///';
echo preg_replace('#[^\w/-]+|/(?=/+$)#', '', $string);

Output: 输出:

controller_123////method/

Demo on 3v4l.org 3v4l.org上的演示

You can get it done with a simple regex where every character except alphanumeric or underscore or hyphen will be replaced with empty string and more than two forward slashes at the end of string will be replaced with single / . 您可以使用简单的正则表达式完成此操作,其中除字母数字,下划线或连字符以外的所有字符都将替换为空字符串,并且在字符串末尾的两个以上的正斜杠将替换为单个/ Just replace this regex, 只需替换此正则表达式,

[^\w-/]+|(/)/+$

With \\1 \\1

Demo 演示

PHP Demo, PHP演示,

$s = "controller_123/method///";
echo preg_replace('@[^\w-/]|(/)/+$@', '\1', $s);

Prints, 打印,

controller_123/method/

Could you please try following regex(considering that your input will be as shown samples only): 您能否尝试遵循正则表达式(考虑到您的输入仅显示为示例):

.*\w\/

Regex DEMO: 正则表达式演示:

In php try to do following. php尝试执行以下操作。

<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
   echo "<h1>Hello, PHP!</h1>\n";
   $string = 'controller_123/method///';
   preg_match('/.*\w\//', $string, $matches);
   print_r($matches);
?>
</body>
</html>

Is giving me following output in online terminal when I test it. 当我对其进行测试时,正在在线终端中给我以下输出。

$php main.php
<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<h1>Hello, PHP!</h1>
Array
(
    [0] => controller_123/method/
)
</body>
</html>

Using regular expression 使用正则表达式

$string=preg_replace("//$/", '', $string );

Using strripos() and strreplace 使用strripos()和strreplace

$string=str_replace(strripos("/",$string),"",$string)

Using rtrim() 使用rtrim()

$string=rtrim("/",$string);

This task can be accomplished with no lookarounds, no capture groups, a single pattern, and a single empty replacement string.这个任务可以在没有环视、没有捕获组、单一模式和单一空替换字符串的情况下完成。

[^-\w/]+   #match one or more non-dash, non-word, non-slash characters
|          #or
/\K/+$     #match a slash, then forget it, them match one or more slash until the end of the string

I'll demonstrate on an array of strings, but preg_replace() will just as happily replace a string as the third parameter.我将在字符串数组上进行演示,但preg_replace()会像第三个参数一样愉快地替换字符串。

Code: ( Demo )代码:(演示

$strings = [
    'controller_123/method///',
    'controller_123/method/',
    '% con $trol@$@ler_!123/me*thod////',
];

var_export(
    preg_replace(
        '~[^-\w/]+|/\K/+$~',
        '',
        $strings
    )
);

Output: Output:

array (
  0 => 'controller_123/method/',
  1 => 'controller_123/method/',
  2 => 'controller_123/method/',
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM