Skip to content

Instantly share code, notes, and snippets.

@hiromi2424
Created June 29, 2011 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiromi2424/1054129 to your computer and use it in GitHub Desktop.
Save hiromi2424/1054129 to your computer and use it in GitHub Desktop.
<?php
$start_time = microtime(true);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<style>
table {
border-collapse: collapse;
}
td {
border: 1px solid black;
}
th {
border-left: 1px solid black;
border-right: 1px solid black;
border-top: 1px solid black;
border-bottom: 1px double black;
}
td, th {
padding: 10px;
}
</style>
</head>
<body>
<form method="get" action="index.php" accept-charset="UTF-8">
<input name="dir" value="<?php echo empty($_GET) ? '' : $_GET['dir'] ?>" type="text" />
<input type="submit" value="submit" />
</form>
<table>
<thead>
<tr><th>ファイル名</th><th>変換</th></tr>
</thead>
<tbody>
<?php
if (!empty($_GET)) {
define('DS', DIRECTORY_SEPARATOR);
$dir = $_GET['dir'];
$files = is_dir($dir) ? files($dir) : array($dir);
foreach ($files as $file) {
echo '<tr><td>', htmlspecialchars($file), '</td><td>';
$contents = file_get_contents($file);
$converted = preg_replace_callback('|<a href="(.+?)">(.*?)<img (.+?) /></a>|u', 'convertLinkImage', $contents);
$converted = preg_replace_callback('|<a href="(.+?)">(.+?)</a>|u', 'convertLink', $converted);
$converted = preg_replace_callback('|<img (.+?) />|u', 'convertImageTag', $converted);
if ($contents !== $converted) {
echo '<span style="color:red; font-weight:bold">変換しました</span>';
file_put_contents($file, $converted);
} else {
echo '変換する場所がありませんでした';
}
echo '</td></tr>';
}
}
function convertLinkImage($matches) {
$href = $matches[1];
$matched = preg_match_all('/([^\s]+?)="(.+?)"/', $matches[3], $attributes);
$data = array();
for ($i = 0; $i < $matched; $i++) {
list($key, $value) = array($attributes[1][$i], $attributes[2][$i]);
$data[$key] = $value;
}
if (!isset($data['src'])) {
echo '<h1 style="text-color:red text-size: 24pt">dumn!!</h1>';
exit;
}
$src = $data['src'];
unset($data['src']);
$src = preg_replace('|^/img/|', '', $src);
$src = preg_replace('|^images/|', '', $src);
$attributes = convertAttributes($data);
if (empty($matches[2])) {
$result = sprintf('<?php echo $this->Html->image(\'%s\', array(\'url\' => \'%s\', %s)) ?>', $src, $href, $attributes);
} else {
$result = sprintf('<?php echo $this->Html->link(\'%s\' . $this->Html->image(\'%s\', array(%s)), \'%s\', array(\'escape\' => false)) ?>', $matches[2], $src, $attributes, $href);
}
return $result;
}
function convertLink($matches) {
list(, $href, $title) = $matches;
$result = sprintf('<?php echo $this->Html->link(\'%s\', \'%s\') ?>', $title, $href);
return $result;
}
function convertImageTag($matches) {
$matched = preg_match_all('/([^\s]+?)="(.+?)"/', $matches[1], $attributes);
$data = array();
for ($i = 0; $i < $matched; $i++) {
list($key, $value) = array($attributes[1][$i], $attributes[2][$i]);
$data[$key] = $value;
}
if (!isset($data['src'])) {
echo '<h1 style="text-color:red text-size: 24pt">dumn!!</h1>';
exit;
}
$src = $data['src'];
unset($data['src']);
$src = preg_replace('|^/img/|', '', $src);
$src = preg_replace('|^images/|', '', $src);
$attributes = convertAttributes($data);
$result = sprintf('<?php echo $this->Html->image(\'%s\', array(%s)) ?>', $src, $attributes);
return $result;
}
function convertAttributes($data) {
$results = array();
foreach ($data as $key => $value) {
$result = "'$key' => ";
if (!is_numeric($value)) {
$value = "'$value'";
}
$result .= "$value";
$results[] = $result;
}
return join(', ', $results);
}
function files($dir) {
$handle = opendir($dir);
if (!$handle) {
return array();
}
$files = array();
while ($file = readdir($handle)) {
if (preg_match('/^\./', $file)) {
continue;
}
$file = str_replace(DS . DS, DS, $dir . DS . $file);
if (is_dir($file)) {
$files = array_merge($files, files($file));
} else {
$files[] = $file;
}
}
return $files;
}
?>
</tbody>
</table>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment