tomasr
tomasr

Reputation: 367

Preg match filenames from html content

This expression find filenames in html content which contains digits, letters and underscores.

preg_match_all('/(\w+\.\w{2,4})/', $content);

But do not find filename like file-name.txt. How can I change expression to find filenames with dash and other legal characters?

Upvotes: 0

Views: 55

Answers (1)

SREagle
SREagle

Reputation: 251

Assuming, the file suffix does not contain non-letters/digits: Doesn't a char class like [\w-] do the trick for you? What exactly did you try?

preg_match_all('/([\w-]+\.\w{2,4})/', $content); works for me for filenames like file-name.txt

Beware: the hyphen has to be at the end, otherwise a range is assumed for the char class by PHP.

Upvotes: 2

Related Questions