Reputation: 1172
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
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
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
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