user1581307
user1581307

Reputation: 15

PHP: find all filename with extension in a text

I'm trying to find all filename with extension in a text. for exemple:

<roottag>
 <step>first element using file_text.txt</step>
 <step>server.sql</step>
 <step>using another.txt file</step>
 <txt> elements other.this </txt>
</roottag>

I would like to extract all filename with extension:

file_text.txt
server.sql
another.txt
other.this

Did you have any idea with preg_match_all?

Thanks in advance

Cris

Upvotes: 1

Views: 1347

Answers (2)

ntrp
ntrp

Reputation: 401

preg_match_all('/[a-z_]+\.[a-z]{2,4}/', $string , $arr, PREG_PATTERN_ORDER);

would match files in you string $string and put an array of matches in $arr.

The regexp string is /[a-z_]+.[a-z]{2,4}/ and it assumes that you are searching for files that are composed by a first letter string that can contain underscores, followed by a point and an extension that can be min 2 char long and max 4 char long.

You can test your regexp string RegExp Tester

so you can add some more rules in the match.

Upvotes: 0

manWe
manWe

Reputation: 350

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

Upvotes: 4

Related Questions