2016-01-19 53 views
0

假設我有一個文件夾中有5個文件。和文件名是一樣sdfsf([email protected])以下如何從多個文件名得到括號之間的字符串?

現在我想從文件名中得到「()」括號之間的字符串,並希望將它們保存在文本文件中。

所以輸出將類似下面

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

我試過這個代碼

<?php 
    foreach (glob("./ABC/*.txt") as $filename) { 
    echo "$filename size " . filesize($filename) . "\n"; 
    } 
?> 
+2

作爲正則表達式,'\((。*)\)'? https://regex101.com/r/gK1yK1/1 – Andrew

回答

1

最佳使用正則表達式進行安德魯指出被保存在一個文本文件中。

$files = scandir('folder/'); 
    foreach($files as $file) { 
    preg_match("/(?:\\((.*)\\))/", $file, $matches); # Or "/\((.*)\)/" by Andrew 
    var_dump($matches); 
    } 
0

您可以使用preg_match通過正則表達式來解決此問題。因此,在這種情況下,使用下面的代碼:

preg_match('/\(.*\)/', $filename, $matches); 
$new_file_name = substr($matches[0],1,strlen($matches[0])-2); //skip the openning and closing parenthesis 

現在$ new_file_name將只包含在括號之間的數據。

希望它有幫助

相關問題