tags:

views:

107

answers:

5

I have an array in PHP with URLs like below:

http://example.com/apps/1235554/
http://example.com/apps/apple/
http://example.com/apps/126734
http://example.com/images/a.jpg
http://example.com/images/b.jpg
http://example.com/apps/2331234/
http://example.com/apps/orange/

How can I separate out these urls and push them to another array using Regex:

http://example.com/apps/1235554/
http://example.com/apps/126734
http://example.com/apps/2331234/

Only url with apps/{number}/ and apps/{number} should be selected.

+4  A: 

Assuming you're not using "/" as the regex separator character to ignore escaping issues, this will do the trick:

   "^http://example.com/apps/\d+/?$"
Jordan Lewis
Will not work as expected on `http://example.com/apps/123aa.jpg`
nailxx
ah, yeah, you need to ensure that there's an end-of-line character at the end. I'll make the edit; thanks.
Jordan Lewis
Don't forget anchors: `"^http://example.com/apps/\d+/?$"` – otherwise it would match nailxx' example.
Blixt
+1  A: 
foreach ($urls as $url)
{
    if (preg_match('~apps/[0-9]~', $url)) echo $url;
}

or more restrictive:

~apps/[0-9]+(/|$)~

to match either slash or the end of the string.

Sjoerd
A: 

^http://example\.com/apps/\d+\bapps then slash, then some digits and then word boundary, i.e. not an alphabetic character.

nailxx
A: 

You can also do it using a negative lookahead to replace all lines that aren't what you want with an empty string:

Replace

http://example\.com/(?!apps/[0-9]+).*

With

''
Bob Fincheimer
A: 

If you want to keep only the URLS with /app/(some digit) and optionally ending with slashes, you can use a loop like this:

for ($i = count($urls); $i-->0 ;) {
    if (!preg_match('-/apps/\d+/?$-', $urls[$i])) unset($urls[$i]);
}   

It cleans your array in-place, without the need to use a temporary one. I'm assuming that the hostname is not constant, otherwise you can change the regexp with:

'-^http://www.example.com/apps/\d+/?$-'

I replaced the standard / regexp delimiter so that I didn't have to escape the slashes of the URL.

Iacopo