Skip to content

Instantly share code, notes, and snippets.

@tperalta82
Last active April 18, 2016 16:35
Show Gist options
  • Save tperalta82/1806adf9c319ee163f5cfbfca17e3fcd to your computer and use it in GitHub Desktop.
Save tperalta82/1806adf9c319ee163f5cfbfca17e3fcd to your computer and use it in GitHub Desktop.
Split an ugly multiple vhost file to multiple files
<?php
if (!isset($argv[1]) || !is_file($argv[1])) {
die("Please specify an input file, preferably one that exists. e.g. php split.php vhosts.conf exportdir(optional) ");
}
if (isset($argv[2]) && is_dir($argv[2])) {
$dir = realpath($argv[2]).DIRECTORY_SEPARATOR;
} else {
echo "No Export dir specified, or non existent one, using default".PHP_EOL;
$dir = getcwd().DIRECTORY_SEPARATOR."vhosts".DIRECTORY_SEPARATOR;
if (!is_dir($dir)) {
mkdir($dir);
}
}
$handle = fopen($argv[1], "r");
$vhstart = "<VirtualHost";
$vhend = "</VirtualHost";
$sname = "ServerName";
$vhost = null;
$filename = null;
while (( $line = \fgets($handle) ) !== false) {
$line = trim($line);
if ($line != "") {
$vhost .= $line.PHP_EOL;
if (stristr($line, $sname)) {
$servername = explode(" ", $line);
$filename = $servername[1].".conf";
} elseif (stristr($line, $vhend)) {
file_put_contents($dir.$filename, $vhost);
$vhost = null;
}
}
}
fclose($handle);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment