php 全文搜索和替换的实现代码

yipeiwu_com6年前PHP代码库
<?php 
exec("/bin/grep -r '$oldword' $rootpath", $results, $errorCode); 
if ($errorCode){ 
if ($errorCode == 1){ 
echo "Possibly no files were found with ?$oldword in them<BR>\n"; 

echo "OS Error: $errorCode<BR>\n"; 
echo "Check 'man errno' and count down<BR>\n"; 
echo "Usually paths/permissions<BR>\n"; 

while (list(,$path) = each($results)){ 
$parts = explode(':', $path); 
$path = $parts[0]; 
$fp = fopen($path, 'r') or print("Cannot read $path<BR>\n"); 
if ($fp){ 
$data = fread($fp, filesize($path)); 
fclose($fp); 
$newdata = str_replace($oldword, $newword, $data); 
$fp = fopen($path, 'w') or print("Cannot write $path<BR>\n"); 
if ($fp){ 
fwrite($fp, $newdata); 
fclose($fp); 
echo $path, "<BR>\n"; 



?> 

相关文章

PHP内核探索:变量存储与类型使用说明

先回答前面一节的那个问题吧。 复制代码 代码如下:<?php    $foo = 10;    $b...

php使用高斯算法实现图片的模糊处理功能示例

php使用高斯算法实现图片的模糊处理功能示例

本文实例讲述了php使用高斯算法实现图片的模糊处理功能。分享给大家供大家参考,具体如下: <?php class image_blur{ function gau...

PHP实现根据设备类型自动跳转相应页面的方法

随着当今移动设备的普及,上网已经比过去更加方便。针对Android智能手机,iPhone/iPad等移动终端,很多网站都相继推出了针对电脑和这类手机等移动设备访问的网页。本文所述的实例代...

PHP之数组学习

今天学习了数组,可以说是PHP的数据应用中较重要的一种方式。PHP的数组函数众多,下面是我学习的小结,借此记之,便于以后鉴之……   一、数组定义:   数组的定义使用 array()方...

PHP使用函数静态变量实现指定迭代次数的方法

本文实例讲述了PHP使用函数静态变量实现指定迭代次数的方法。分享给大家供大家参考,具体如下: 在PHP中,除了类的静态成员属性外,在函数中同样可以利用 static 定义静态变量。从而便...