giuseppe giuseppe - 5 months ago 10
PHP Question

Trouble With Regexp

I have to replace matches of patterns like

<something:any-char>
within a
URL
.
For example, a
URL
like this:

http://some-site.com/some-acion/pippo:1/mypar:asdasd/pippo2:sdd/ .....


should become:

http://some-site.com/some-acion/pippo:1/pippo2:sdd/ .....


In other words, I have to filter out any occurrence of
mypar:
from the URL.
I will use php for that.
I tried with RegExp:

.*[\/]+(sh:.*)[\/]?.*$


But it matches only strings like
/pippo:3/mypar:wdfds
. Strings like
/pippo:2/mypar:asa/7pippo:1/
are not matched.
Any hint appreciated.

Answer

You could do this:

$url = "/pippo:2/mypar:asa/7pippo:1/";
$stripped = preg_replace("/\/mypar:.*?(\/|$)/", "$1", $url);