php搜索任何wav文件名的文本文件

php搜索任何wav文件名的文本文件,php,regex,search,Php,Regex,Search,我有一系列包含原始文本或json数据的文件,在这些文件中会有wav文件名。所有wav文件的后缀均为.wav 是否使用php我可以搜索单个文本或json文件并返回找到的任何.wav文件的数组 这个随机文本示例包含6.wav文件,如何搜索并提取文件名 Spoke as as other again ye. Hard on to roof he drew. So sell side newfile.wav ye in mr evil. Longer waited mr of nature seemed

我有一系列包含原始文本或json数据的文件,在这些文件中会有wav文件名。所有wav文件的后缀均为
.wav

是否使用php我可以搜索单个文本或json文件并返回找到的任何
.wav
文件的数组

这个随机文本示例包含6.wav文件,如何搜索并提取文件名

Spoke as as other again ye. Hard on to roof he drew. So sell side newfile.wav ye in mr evil. Longer waited mr of nature seemed. Improving knowledge incommode objection me ye is prevailed playme.wav principle in. Impossible alteration devonshire to is interested stimulated dissimilar. To matter esteem polite do if. 

Spot of come to ever test.wav hand as lady meet on. Delicate contempt received two yet advanced. Gentleman as belonging he commanded believing dejection in by. On no am winding chicken so behaved. Its preserved sex enjoyment new way behaviour. Him yet devonshire celebrated welcome.wav especially. Unfeeling one provision are smallness resembled repulsive. 

Raising say express had chiefly detract demands she. Quiet led own cause three him. Front no party young abode state up. Saved he do fruit woody of to. Met defective are allowance two perceived listening consulted contained. It chicken oh colonel pressed excited suppose to shortly. He improve started no we manners another.wav however effects. Prospect humoured mistress to by proposal marianne attended. Simplicity the far admiration preference everything. Up help home head spot an he room in. 

Talent she for lively eat led sister. Entrance strongly packages she out rendered get quitting denoting led. Dwelling confined improved it he no doubtful raptures. Several carried through an of up attempt gravity. Situation to be at offending elsewhere distrusts if. Particular use for considered projection cultivated. Worth of do doubt shall it their. Extensive existence up me last.wav contained he pronounce do. Excellence inquietude assistance precaution any impression man sufficient. 
我试过了,但没有结果

$lines = file('test.txt');

foreach ($lines as $line_num => $line) {

    $line = trim($line);

    if (strpos($line, '*.wav') !== false) {
        echo ($line);
    }

}
上述文本应返回:

newfile.wav
playme.wav
test.wav
welcome.wav
another.wav
last.wav
谢谢

更新:

使用以下命令:

$text = file_get_contents('test.txt');
preg_match_all('/\w+\.wav/', $text, $matches);
var_dump($matches);
结果是一系列:

    array(1) {
      [0]=>
      array(6) {
        [0]=>
        string(11) "newfile.wav"
        [1]=>
        string(10) "playme.wav"
        [2]=>
        string(8) "test.wav"
        [3]=>
        string(11) "welcome.wav"
        [4]=>
        string(11) "another.wav"
        [5]=>
        string(8) "last.wav"
      }
}
所以一个wav文件数组,在一个数组中,我如何得到wav文件数组?谢谢

对于名称中带有空格的wav文件,这无法正常工作。 有什么想法吗?

这就是我们被发明的原因。 一些好的资源:

  • 允许您实时测试表达式
输出:

    array(6) {
      [0] => string(11) "newfile.wav"
      [1] => string(10) "playme.wav"
      [2] => string(8) "test.wav"
      [3] => string(11) "welcome.wav"
      [4] => string(11) "another.wav"
      [5] => string(8) "last.wav"
    }

你就快到了。您可以按空格分解
$行
。现在,检查每个单词是否以
.wav
扩展名结尾。如果是,则打印单词

<?php

foreach ($lines as $line_num => $line) {
    $line = trim($line);
    $words = explode(" ",$line);
    foreach($words as $each_word){
        $wav_index = strpos($each_word, '.wav');
        if ($wav_index !== false && $wav_index === strlen($each_word) - 4) { // strict check to make sure string ends with a .wav and not being elsewhere
            echo $each_word,PHP_EOL;
        }
    }    
}
可能会帮助您根据需要设计表达式并对其进行测试,可能类似于:

([a-z]+\.wav)
如果您愿意,还可以为其添加更多边界

这里]]

图表 此图显示了表达式的工作方式,您可以通过以下方式可视化其他表达式:

PHP代码 您也可以使用
preg\u match\u all
来执行此操作,可能类似于:

$re = '/([a-z]+\.wav)/m';
$str = 'Spoke as as other again ye. Hard on to roof he drew. So sell side newfile.wav ye in mr evil. Longer waited mr of nature seemed. Improving knowledge incommode objection me ye is prevailed playme.wav principle in. Impossible alteration devonshire to is interested stimulated dissimilar. To matter esteem polite do if.

    Spot of come to ever test.wav hand as lady meet on. Delicate contempt received two yet advanced. Gentleman as belonging he commanded believing dejection in by. On no am winding chicken so behaved. Its preserved sex enjoyment new way behaviour. Him yet devonshire celebrated welcome.wav especially. Unfeeling one provision are smallness resembled repulsive.

    Raising say express had chiefly detract demands she. Quiet led own cause three him. Front no party young abode state up. Saved he do fruit woody of to. Met defective are allowance two perceived listening consulted contained. It chicken oh colonel pressed excited suppose to shortly. He improve started no we manners another.wav however effects. Prospect humoured mistress to by proposal marianne attended. Simplicity the far admiration preference everything. Up help home head spot an he room in.

    Talent she for lively eat led sister. Entrance strongly packages she out rendered get quitting denoting led. Dwelling confined improved it he no doubtful raptures. Several carried through an of up attempt gravity. Situation to be at offending elsewhere distrusts if. Particular use for considered projection cultivated. Worth of do doubt shall it their. Extensive existence up me last.wav contained he pronounce do. Excellence inquietude assistance precaution any impression man sufficient. ';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);
正则表达式的测试脚本
const regex=/([a-z]+\.wav)/gm;
const str=`你又像其他人一样说话了。他使劲爬上屋顶。因此,在邪恶先生中出售newfile.wav ye。等待的时间似乎更长了。提高知识不妨碍我的进步是游戏的原则。不可能的改变德文郡是不同的。如有必要,做某事。
来过测试的地点。女士见面时挥手。微妙的轻蔑受到了两次进一步的攻击。他命令人们相信他所属的绅士是沮丧的。我从来没有这样表现过。它保留了性享受和新的行为方式。然而德文郡却特别欢迎他。无情的一条规定是小得令人厌恶。
提高赛义特快车的价格主要是降低了她的要求。他沉默了三次。前面没有党的年轻人居住状态了。救了他,他做了木本的水果。符合缺陷的公差包含两个。我想很快就会的。他一开始就不懂礼貌,但效果很好。玛丽安参加的求婚使这位潜在的情人很高兴。简单是一切事物的首选。帮助家里的头在房间里找到一个他。
才华横溢的她为活泼好动的姐姐吃过饭。入口强烈包裹,她退出,表示退出。毫无疑问,他欣喜若狂。有几个人进行了重力上升的尝试。在其他地方冒犯他人的情况是不信任的。特殊用途,用于特定用途。值得怀疑的是,这将是他们的梦想。广泛存在于我最后。wav包含他发音do。卓越、探究、协助、预防、任何印象都是足够的;
让m;
while((m=regex.exec(str))!==null){
//这是避免具有零宽度匹配的无限循环所必需的
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
//可以通过'm`-变量访问结果。
m、 forEach((匹配,组索引)=>{
log(`Found match,group${groupIndex}:${match}`);
});

}
if(strpos($line,*.wav')
更改为
if(strpos($line,.wav')
也可以打印整行?使用
preg\u match\u all('~\S+\.wav\b~,$text,$matches)
,如果
print\r($matches[0]),您可以看到结果
。您需要将文件作为单个字符串读入,但要使其工作,请使用
$text=file\u get\u contents('test.txt');
。可能应该使用preg_match_all。为什么对此投反对票?它似乎有效,我很好奇它是否会引起问题?它确实有效,但提供了数组中的文件数组。如何仅返回文件数组?@Rocket use$matches[0]因此,如果您不需要这些位置,@PeterRakmanyi谢谢,这很好,除非wav文件的名称中有空格。有什么办法处理吗?在空格处分解行,然后搜索是否会增加额外的负载?@Tom它可能需要一些额外的内存,但可以忽略不计。@vivek_23我的问题是wav文件可能有空格有名字。有办法处理吗?@Rocket你能告诉我你是如何得到这些数据的吗?这对我来说似乎不起作用。我也不明白为什么我仍然没有得到这个工作。它将如何处理名字中有空格的文件?
$re = '/([a-z]+\.wav)/m';
$str = 'Spoke as as other again ye. Hard on to roof he drew. So sell side newfile.wav ye in mr evil. Longer waited mr of nature seemed. Improving knowledge incommode objection me ye is prevailed playme.wav principle in. Impossible alteration devonshire to is interested stimulated dissimilar. To matter esteem polite do if.

    Spot of come to ever test.wav hand as lady meet on. Delicate contempt received two yet advanced. Gentleman as belonging he commanded believing dejection in by. On no am winding chicken so behaved. Its preserved sex enjoyment new way behaviour. Him yet devonshire celebrated welcome.wav especially. Unfeeling one provision are smallness resembled repulsive.

    Raising say express had chiefly detract demands she. Quiet led own cause three him. Front no party young abode state up. Saved he do fruit woody of to. Met defective are allowance two perceived listening consulted contained. It chicken oh colonel pressed excited suppose to shortly. He improve started no we manners another.wav however effects. Prospect humoured mistress to by proposal marianne attended. Simplicity the far admiration preference everything. Up help home head spot an he room in.

    Talent she for lively eat led sister. Entrance strongly packages she out rendered get quitting denoting led. Dwelling confined improved it he no doubtful raptures. Several carried through an of up attempt gravity. Situation to be at offending elsewhere distrusts if. Particular use for considered projection cultivated. Worth of do doubt shall it their. Extensive existence up me last.wav contained he pronounce do. Excellence inquietude assistance precaution any impression man sufficient. ';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);