简体   繁体   English

在两个斜杠之间替换字符串

[英]Replace string between two slashes

I have to modify an URL like this: 我必须像这样修改网址:

$string = "/st:1/sc:RsrlYQhSQvs=/fp:1/g:3/start:2015-07-01/end:2015-07-30";

Namely, I want to delete st:1 with a regex. 即,我想用正则表达式删除st:1 I used: 我用了:

preg_replace("/\/st:(.*)\//",'',$string)

but I got 但是我得到了

end:2015-07-30

while I would like to get: 而我想得到:

/sc:RsrlYQhSQvs=/fp:1/g:3/start:2015-07-01/end:2015-07-30

Same if I would like to delete fp:1 . 同样,如果我想删除fp:1

You are using greedy matching with . 您正在与进行贪婪匹配. that matches any character. 匹配任何字符。

Use a more restricted pattern: 使用更严格的模式:

preg_replace("/\/st:[^\/]*/",'',$string)

The [^\\/]* negated character class only matches 0 or more characters other than / . [^\\/]*否定字符类仅匹配/以外的0个或更多字符

Another solution would be to use lazy matching with *? 另一个解决方案是使用带*?惰性匹配*? quantifier, but it is not that efficient as with the negated character class. 量词,但效率不如否定字符类。

FULL REGEX EXPLANATION : 完整的正则表达式说明

  • \\/st: - literal /st: \\/st: -文字/st:
  • [^\\/]* - 0 or more characters other than / . [^\\/]* - /以外的0个或更多字符。

You can use: 您可以使用:

$string = preg_replace('~/st:[^/]*~','',$string);

[^/]* will only match till next / [^/]*仅匹配到下一个/

You need to add ? 您需要添加? in your regex:- 在您的正则表达式中:-

<?php

$string = "/st:1/sc:RsrlYQhSQvs=/fp:1/g:3/start:2015-07-01/end:2015-07-30";
echo preg_replace("/\/st:(.*?)\//",'',$string)

 ?>

Output:- https://eval.in/397658 输出: -https : //eval.in/397658

Based on this same you can do for next things also. 基于这一点,您也可以为接下来的事情做。

Instead of using regex here you should make parsing utility functions for your special format string, they are simple, they don't take to long to write and they will make your life a lot easier: 不用在这里使用正则表达式,您应该为特殊格式的字符串创建解析实用程序函数,它们很简单,不需要花费很长时间编写,它们会使您的生活变得更加轻松:

function readPath($path) {
    $parameters = array();

    foreach(explode('/', $path) as $piece) {
        // Here we make sure we have something
        if ($piece == "") {
            continue;
        }

        // This here is just a fancy way of splitting the array returned
        // into two variables.
        list($key, $value) = explode(':', $piece);
        $parameters[$key] = $value;
    }

    return $parameters;
} 

function writePath($parameters) {
    $path = "";

    foreach($parameters as $key => $value) {
        $path .= "/" . implode(":", array($key, $value));
    }

    return $path;
}

Now you can just work on it as a php array, in this case you would go: 现在,您可以将其作为php数组进行处理,在这种情况下,您可以执行以下操作:

$parameters = readPath($string);

unset($parameters['st']);

$string = writePath($parameters);

This makes for much more readable and reusable code, additionally since most of the time you are dealing with only slight variations of this format you can just change the delimiters each time or even abstract these functions to using different delimiters. 这使得代码更具可读性和可重用性,此外,由于大多数时候您只在处理这种格式的细微变化,因此每次都可以更改定界符,甚至可以将这些函数抽象为使用不同的定界符。


Another way to deal with this is to convert the string to conform to a normal path query, using something like: 解决此问题的另一种方法是使用类似以下的方法将字符串转换为符合常规路径查询的格式:

function readPath($path) {
    return parse_str(strtr($path, "/:", "&="));
}

In your case though since you are using the "=" character in a url you would also need to url encode each value so as to not conflict with the format, this would involve similarly structured code to above though. 在您的情况下,尽管由于您在url中使用了“ =”字符,所以您还需要对每个值进行url编码,以免与格式冲突,但这将涉及与上面类似的结构化代码。

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

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