2012-03-10 86 views

回答

1

我自己找不到这个地方,所以我编写了一个快速和肮脏的PHP hack来做(刚才)。这绝不是一个优雅的解决方案,但对我的目的来说已经足够好了。在最后注释copy部分并用一些echo构造替换它以确保您的代码复制正确的文件可能是一个好主意。

帮助条目:

Usage example: ./copy_xspf.php --e -i myMusic.xspf -o /home/foo/bar 
-i filename or --input filename - XSPF file to parse 
-o directory or --input directory - directory to copy to (needs to exist!) 
--help - display this help and terminate 

脚本来源:

#!/usr/bin/php 
<?php 
// Quick and dirty hack. 

for($i = 1; $i < count($argv); ++$i) { 
    if($argv[$i] == '--help' || $argv[$i] == '-e') { 
     echo "Usage example: ./copy_xspf.php --e -i myMusic.xspf -o /home/foo/bar\n"; 
     echo "-i filename or --input filename - XSPF file to parse\n"; 
     echo "-o directory or --input directory - directory to copy to (needs to exist!)\n"; 
     echo "--help - display this help and terminate\n"; 
     die(); 
    } 
    if($argv[$i] == '--input' || $argv[$i] == '-i') { 
     if(!isset($argv[$i+1])) { 
      die("No input filename given (xspf file), use -i filename or --input filename\n"); 
     } else { 
      $filename = $argv[$i+1]; 
     } 
    } 
    if($argv[$i] == '--output' || $argv[$i] == '-o') { 
     if(!isset($argv[$i+1])) { 
      die("No output directory given, use -o directory or --output directory\n"); 
     } else { 
      $outputDir = $argv[$i+1]; 
     } 
    } 
} 
if(!isset($filename) || empty($filename)) { 
    die("No input filename given (xspf file), use -i filename or -input filename\n"); 
} 
if(!isset($outputDir) || empty($outputDir)) { 
    die("No output directory given, use -o directory or --output directory\n"); 
} else { 
    $outputDir = rtrim($outputDir, '/'); 
} 

$xml = file_get_contents($filename); 

preg_match_all('#<location>(.*?)</location>#', $xml, $matches); 
$matches = $matches[1]; // Select only the contents of (.*?), not the entire pattern 
$matches = preg_replace('#file://(.*)#', '\\1', $matches); // Remove file:// 
foreach($matches as $key => $value) { 
    $matches[$key] = urldecode($value); 
    $matches[$key] = html_entity_decode($matches[$key]); 
} 


foreach($matches as $value) { 
    $base = basename($value); 
    echo "Copying $base ...\n"; 
    copy($value, "$outputDir/$base"); 
} 
相关问题