简体   繁体   English

PHP:在文本中查找带扩展名的所有文件名

[英]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? 你对preg_match_all有什么想法吗?

Thanks in advance 提前致谢

Cris 克里斯

$preg = '/(\w+\.\w{2,4})/';
preg_match_all($preg, $text);
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. 将匹配您的字符串$ string中的文件并在$ arr中放置一系列匹配项。

The regexp string is /[a-z_]+.[az]{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. regexp字符串是/[a-z_]+。[az] {2,4} /并且它假定您正在搜索由可以包含下划线的第一个字母字符串组成的文件,后跟一个点和一个扩展名可以是最小2个字符长,最多4个字符长。

You can test your regexp string RegExp Tester 您可以测试regexp字符串RegExp Tester

so you can add some more rules in the match. 所以你可以在比赛中添加更多规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM