php替换文件中的数据库,批量替换php文件中的class,id的值
批量替換php文件中的class,id的值
$val="wqewq
替換成"wqewq
//其實也就是在class,id值后面加上一個字符串
//我要替換的是文件里的。所以要從文件讀取出來
//我自己寫了一個但是不行類似這樣
$fp=fopen("tpl_checkout_stacked.php","rb");
$content=fread($fp,filesize("tpl_checkout_stacked.php"));
$content=ereg_replace(" id='(.*)'"," id='\\1_test'",$content);
$content=ereg_replace(" class='(.*)'"," class='\\1_test'",$content);
echo($content);
fclose($fp);
$fp=fopen("test.php","a");
fwrite($content,$fp);
$filepath = '/path/to/file';
$content = file_get_contents($filepath);
替換
file_put_contents($filepath, $content);
// +------ 我以前寫的一個替換函數(shù)
function file_modify($search_contents, $replace_contents, $filename)
{
$fp = file_get_contents($filename);
$new_fp = str_replace($search_contents, $replace_contents, $fp);
file_put_contents($filename, $new_fp);
}
// +------ 用法
file_modify('sdf hjhj', 'sdf_test hjhj_test', 'test10.html');
preg_replace("/(class='([^\s]+)\s+([^']+)/gi","class='$1_test $2_test'",$content);
preg_replace("/id='([^\s]+)\s+([^']+)/","id='$1_test $2_test'",$content);
總結(jié)
以上是生活随笔為你收集整理的php替换文件中的数据库,批量替换php文件中的class,id的值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php的反射技术,PHP 反射使用
- 下一篇: matlab调用q外部exe文件,Mat