Pitu
Pitu

Reputation: 159

PHP preg_match url

I have a problem with preg_match in PHP.

I have URLs:

http://mysite.com/file/w867/2612512232
http://mysite.com/file/c3233/2123255464
etc.

I need URLs:

http://mysite.com/file/2612512232
http://mysite.com/file/2123255464

I must remove:

w867/
c3233/
etc.

Upvotes: 0

Views: 1123

Answers (3)

Pawka
Pawka

Reputation: 2576

preg_replace("|file/\w+/|", "file/", $url);

This will search for 1st pattern between "/" symbols just after "file/" part.

Upvotes: 0

Daniel Castro
Daniel Castro

Reputation: 633

You don't necessarily need to use preg_match. parse_url() can do the job.

http://us.php.net/manual/en/function.parse-url.php

Just make sure to concatenate it all against without the part you don't want.

Upvotes: 2

khael
khael

Reputation: 2610

you could try a pattern like this:

"/(.*)\/([^/])*\/(.*)/"

and then with str_replace you can: $string = str_replace('/'.$matches[2], '', $string);

Upvotes: 0

Related Questions