开发者

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 al开发者_StackOverflow社区ways 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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜