在两个斜杠之间替换字符串
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. 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
.
我必须修改这样的URL: p>
$ string =“/ st:1 / sc:RsrlYQhSQvs = / fp:1 / g:3 / start:2015-07-01 / end:2015-07-30”;
code> pre>
即,我想用正则表达式删除 st:1 code>。 我用过: p>
preg_replace(“/ \ / st:(。*)\ //”,'',$ string)
code> pre>
但我得到 p>
end:2015-07-30
code> pre>
我想得到: p>
/ sc:RsrlYQhSQvs = / fp:1 / g:3 / start:2015-07-01 / end:2015-07-30
code> pre>
如果我想删除 fp:1 code>,则相同。 p>
div>
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
Based on this same you can do for next things also.
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 /
.
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:
-
[^\/]*
- 0 or more characters other than/
.
You can use:
$string = preg_replace('~/st:[^/]*~','',$string);
[^/]*
will only match till next /
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:
$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.