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中rename()函数的妙用讲解

大家都知道,rename()函数可以对文件或目录进行重命名的操作。其实它还可以做很多事情。 熟悉unix的朋友应该知道shell命令mv,它相当与win32的移动,而且移动的同时可进行重...

PHP中::、-&amp;gt;、self、$this几种操作符的区别介绍

在访问PHP类中的成员变量或方法时,如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就必须使用操作符::,反之如果被引用的变量或者方法没有被声明成...

PHP7匿名类用法分析

PHP7匿名类用法分析

本文实例讲述了PHP7匿名类用法。分享给大家供大家参考,具体如下: 匿名类跟匿名函数一样,创建一次性的简单对象 <?php /** * Created by PhpS...

php页面跳转代码 输入网址跳转到你定义的页面

代码很简单,涉及到header函数的使用,代码如下 复制代码 代码如下: <?php if (!empty($_SERVER['HTTPS']) && ('on' == $_SER...

PHP处理Oracle的CLOB实例

本文实例简述了PHP处理Oracle的CLOB的方法。分享给大家供大家参考。具体方法如下: 1. 写入数据   在使用PDO的预处理方法时,如果使用bindParam()等而不...