views:

62

answers:

3

I am trying to fix a regular expression i have been using in php it finds all find filenames within a sentence / paragraph. The file names always look like this:

/this-a-valid-page.php

From help i have received on SOF my old pattern was modified to this which avoids full urls which is the issue i was having, but this pattern only finds one occurance at the beginning of a string, nothing inside the string.

/^\/(.*?).php/ 


I have a live example here: http://vzio.com/upload/reg_pattern.php

+1  A: 

Remove the ^ - the carat signifies the beginning of a string/line, which is why it's not matching elsewhere.

If you need to avoid full URLs, you might want to change the ^ to something like (?:^|\s) which will match either the beginning of the string or a whitespace character - just remember to strip whitespace from the beginning of your match later on.

Amber
That expression is what i was trying to figure out... my reg skills are horrible. thanks, it works exactly how i wanted $pattern = "/(?:^|\s)\/(.*?)\.php/";
jason
A: 

To find all the occurrences, you'll have to remove the start anchor and use the preg_match_all function instead of preg_match :

if(preg_match_all('/\/(.*?)\.php/',$input,$matches)) {
 var_dump($matches[1]); // will print all filenames (after / and before .php)
}

Also . is a meta char. You'll have to escape it as \. to match a literal period.

codaddict
+2  A: 

The last dot in your expression could still cause problems, since it'll match "one anything". You could match, for example, /somefilename#php with that pattern. Backslash it to make it a literal period:

/\/(.*?)\.php/

Also note the ? to make .* non-greedy is necessary, and Arda Xi's pattern won't work. .* would race to the end of the string and then backup one character at a time until it can match the .php, which certainly isn't what you'd want.

sweaver2112
You were right, it did ignore spaces and try and find the next .php
jason