Скпипт для рекурсивной замены текста в файлах.
06.09.2016
Маленький скрипт, полезен если нужно произвести массовую замену в файлах.
go.php (Download)
<?
class soReplacer
{
protected $_sWhat = '';
protected $_sForWhat = '';
/**
* ������
*
* @param string $sFilePath
*
* @return void
*/
protected function _replace($sFilePath)
{
file_put_contents( $sFilePath,
str_replace($this->_sWhat, $this->_sForWhat,
file_get_contents($sFilePath)
)
);
}
/**
* ������� ��
*
* @param string $sPath
*
* @return void
*/
protected function _scan($sPath)
{
$d = dir($sPath);
while (false !== ($entry = $d->read()))
{
$sCurrPath = $sPath.'/'.$entry;
if ($entry != '.' && $entry != '..')
{
if (is_dir($sCurrPath))
{
$this->_scan($sCurrPath);
//-- rmdir ($sCurrPath);
}
else
{
echo $sCurrPath."\n"; // <br />
//-- unlink($sCurrPath);
$this->_replace($sCurrPath);
}
}
}
$d->close();
}
/**
* Init
*
* @param string $sPath
* @param string $sWhat
* @param string $sForWhat
*
* @return void
*/
public function go($sPath, $sWhat, $sForWhat)
{
$this->_sWhat = $sWhat;
$this->_sForWhat = $sForWhat;
$this->_scan( $sPath );
}
}
$oReplacer = new soReplacer();
$oReplacer->go(
__DIR__,
'/zzz2',
'/zzz2'
);
TODO: добавить проверку, что в файле есть что заменять, а только потом выполнять замену.