Last active
August 29, 2015 14:05
-
-
Save flangofas/3778e3449c1ad7f23b72 to your computer and use it in GitHub Desktop.
Find and Replace using CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FindReplace{ | |
const TOKEN = '&&'; | |
const HYPHEN = '-'; | |
const TARGETDIR = 'modified'; | |
const TEMPLATESDIR = 'templates'; | |
public $data = array(); | |
public $filename; | |
public $html; | |
public $translations; | |
public $currentlang; | |
public function __construct($file, $csv = null) | |
{ | |
$this->filename = $file; | |
$this->data = self::getCSV($csv); | |
if (!empty($this->data)) { | |
self::process(); | |
} | |
} | |
public function process() | |
{ | |
$langs = static::getLangs(); | |
foreach ($langs as $l) { | |
$this->currentlang = $l; | |
self::replace(); | |
} | |
} | |
static private function getLangs() | |
{ | |
$result = array ( | |
'en', | |
'ar', | |
'zh-hans' | |
); | |
return $result; | |
} | |
public function getCSV($csv) | |
{ | |
$result = array(); | |
$langs = static::getLangs(); | |
$handle = fopen($csv, "r"); | |
if ($handle) { | |
while (($row = fgetcsv($handle,'4069')) !== false) { | |
$i = 0; | |
foreach ($row as $r=>$column) { | |
if ($i === 0) { | |
$key = strtolower($column); | |
} | |
$result[$key][$langs[$i]] = $column; | |
$i++; | |
} | |
} | |
if (!feof($handle)) { | |
die("Error: unexpected fgets() fail\n"); | |
} | |
fclose($handle); | |
} | |
//self::pre($result, 'check csv'); | |
return $result; | |
} | |
function findReplacements() | |
{ | |
$result = array(); | |
$filename = static::TEMPLATESDIR . DIRECTORY_SEPARATOR . $this->filename; | |
if (file_exists($filename)) { | |
$this->html = file_get_contents($filename); | |
//replace double quotes with single quotes | |
$this->html = preg_replace("#\"#", "'", $this->html); | |
preg_match_all('#\&\&(.*)\&\&#', $this->html,$matches); | |
$result = $matches[1]; | |
} else { | |
die ("Error $this->filename does't exist.") ; | |
} | |
return $result; | |
} | |
function getReplacements() | |
{ | |
$result = array (); | |
$replacements = self::findReplacements(); | |
foreach ($replacements as $key) { | |
$key = strtolower($key); | |
if (self::isTranslated($key) === true) { | |
$result[$key] = $this->data[$key][$this->currentlang]; | |
} | |
} | |
return $result; | |
} | |
function replace() | |
{ | |
$to_be_replaced = self::getReplacements(); | |
foreach ($to_be_replaced as $key=>$translation) { | |
$pattern = '#'. static::TOKEN . preg_quote($key). static::TOKEN .'#i'; | |
$this->html = preg_replace($pattern, $translation, $this->html); | |
} | |
$file = explode('.',$this->filename); | |
$output = static::TARGETDIR . DIRECTORY_SEPARATOR . $this->currentlang . DIRECTORY_SEPARATOR . $file[0]; | |
$output .= static::HYPHEN . $this->currentlang . '.html'; | |
if (!file_put_contents($output, $this->html)) { | |
die('Error occured. '); | |
} | |
} | |
function isTranslated($key) | |
{ | |
$result = false; | |
if (!empty($this->data[$key][$this->currentlang])) { | |
$result = true; | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment