2012-03-10 3 views

答えて

1

私自身どこか他の場所でこれを見つけることができなかったので、これを行うための迅速で汚れたPHPハックをコーディングしました。これは決してエレガントな解決策ではありませんが、私の目的には十分です。最後に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"); 
} 
関連する問題