我是PHP和正则表达式的新手,阅读后我已经尝试了解如何提取正确的信息,
示例数据
2011/09/20 00:57 367,044,608 S1E04 - Cancer Man.avi
2012/03/12 03:01 366,991,496 Family Guy - S09E01 - And Then There Were Fewer.avi
2012/03/25 00:27 53,560,510 Avatar- The Legend of Korra S01E01.avi
我想要提取的是文件的日期,文件大小和名称,记住文件基本上可以从任何内容开始。和文件大小一直在变化。
我现在有什么。
$dateModifyed = substr($file, 0, 10);
$fileSize = preg_match('[0-9]*/[0-9]*/[0-9]*/s[0-9]*:[0-9]*/s*', $file, $match)
$FileName =
我正在处理的完整代码
function recursivePrint($folder, $subFolders, $Jsoncounter) {
$f = fopen("file.json", "a");
echo '{ "id" : "' . $GLOBALS['Jsoncounter'] . '", parent" : "' . "#" . '", Text" : "' . $folder . '" },' . "\n";
$PrintString = '{ "id" : "' . $GLOBALS['Jsoncounter'] . '", parent" : "' . "#" . '", Text" : "' . $folder . '" },' . "\n";
fwrite($f, $PrintString);
$foldercount = $GLOBALS['Jsoncounter'];
$GLOBALS['Jsoncounter']++;
foreach($subFolders->files as $file) {
preg_match('/^(\d{4}/\d{2}/\d{2}\s+\d{2}:\d{2})\s+([\d,]+)\s+(.*)$/', $file, $match);
$dateModified = $match[1];
$fileSize = str_replace(',', '', $match[2]);
$fileName = $match[3];
echo $dateModified . $fileSize . $fileName;
echo '{ "id" : "' . $GLOBALS['Jsoncounter'] . '", parent" : "' . $foldercount . '", Text" : "' . $file . '" },';
$PrintString ='{ "id" : "' . $GLOBALS['Jsoncounter'] . '", parent" : "' . $foldercount . '", Text" : "' . $file . '" },';
fwrite($f, $PrintString);
$GLOBALS['Jsoncounter']++;
}
foreach($subFolders->folders as $folder => $subSubFolders) {
recursivePrint($folder, $subSubFolders, $Jsoncounter);
}
fclose($f);
}
非常感谢任何提取正确数字的帮助
答案 0 :(得分:1)
你的正则表达式有几个问题:
preg_match('[0-9]*/[0-9]*/[0-9]*/s[0-9]*:[0-9]*/s*', $file, $match)
^--missing delimiter ^ ^-- asterisk instead of plus
|--literal s instead of \s
当然您还没有使用anchors或capturing groups,而且正则表达式尚未完成。
尝试以下方法:
preg_match_all(
'%^ # Start of line
([0-9]+/[0-9]+/[0-9]+) # Date (group 1)
\s+ # Whitespace
([0-9]+:[0-9]+) # Time (group 2)
\s+ # Whitespace
([0-9,]+) # File size (group 3)
\s+ # Whitespace
(.*) # Rest of the line%mx',
$file, $result, PREG_SET_ORDER);
for ($matchi = 0; $matchi < count($result); $matchi++) {
for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
# Matched text = $result[$matchi][$backrefi];
所以例如$result[0][1]
将包含2011/09/20
,$result[2][4]
将包含Avatar- The Legend of Korra S01E01.avi
等。
答案 1 :(得分:1)
您需要使用捕获组来获取正则表达式的不同部分匹配的字符串部分。捕获组在正则表达式的各个部分周围使用括号。
preg_match('#^(\d{4}/\d{2}/\d{2}\s+\d{2}:\d{2})\s+([\d,]+)\s+(.*)$#', $string, $match);
$dateModified = $match[1];
$fileSize = str_replace(',', '', $match[2]);
$fileName = $match[3];
正则表达式中的其他问题:
/s
代替\s
用于空格字符。www.regular-expressions.info上有关于正则表达式的教程。