jason
jason

Reputation: 1172

php regular expression help finding multiple filenames only not full URL

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

Upvotes: 1

Views: 272

Answers (3)

codaddict
codaddict

Reputation: 455092

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.

Upvotes: 0

Scott Weaver
Scott Weaver

Reputation: 7361

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.

Upvotes: 2

Amber
Amber

Reputation: 526643

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.

Upvotes: 2

Related Questions