PHP Bulk Replace content or all file contents under a specified directory _php tutorial

Source: Internet
Author: User
To replace the contents of a string we just use PHP-related functions, such as strstr,str_replace, regular expressions, then we have to replace the contents of all files in the directory will need to go through the directory and then open the file and then use the above function replaced. First we look at the most basic

The STRTR () function converts a specific character in a string.

Grammar
STRTR (string,from,to) or

STRTR (String,array)

*/

The code is as follows Copy Code

$addr =strtr ($addr, "", "Aao"); The first method of invocation
$trans =array ("Hello" = "hi", "hi" = "Hello"); Defining a Transform Array
echo strtr ("Hi all, I said hello", $trans); The second method of invocation


Normal string

Echo strtr ("Hilla Warld", "ia", "eo");

Array

$arr = Array ("Hello" = "hi", "World" = "Earth");
echo strtr ("Hello World", $arr);

/*
If the from and to lengths are different, the format is the shortest length.

Let's look at a simple function.

+------A replacement function I wrote previously

The code is as follows Copy Code

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);
}

+------Usage
File_modify (' SDF hjhj ', ' sdf_test hjhj_test ', ' test10.html ');


or directly with

Or just use this.

The code is as follows Copy Code

Preg_replace (' | () (^<]+) () |isu ', "${1}". The content after the replacement. "$", $str);

Preg_replace (' | () (^<]+) () |isu ', "${1}". The content after the replacement. "$", $str);


All the questions I said above will only replace a file inside, then I want to replace a site all directories in the file specified characters, then we look at the following function

The code is as follows Copy Code
if (Isset ($_get[' dir ')) {//Set file directory
$basedir =$_get[' dir '];
}else{
$basedir = '. ';
}
$auto = 1;
Checkdir ($basedir);
function Checkdir ($basedir) {
if ($dh = Opendir ($basedir)) {
while (($file = Readdir ($DH))!== false) {
if ($file! = '. ' && $file! = ' ... ') {
if (!is_dir ($basedir. " /". $file)) {
echo "FileName: $basedir/$file". Checkbom ("$basedir/$file"). "
";
}else{
$dirname = $basedir. " /". $file;
Checkdir ($dirname);
}
}
}
Closedir ($DH);
}
}
function Checkbom ($filename) {
Global $auto;
$contents = file_get_contents ($filename);
$charset [1] = substr ($contents, 0, 1);
$charset [2] = substr ($contents, 1, 1);
$charset [3] = substr ($contents, 2, 1);
if (Ord ($charset [1]) = = 239 && ord ($charset [2]) = = 187 && ord ($charset [3]) = = 191) {
if ($auto = = 1) {
$rest = substr ($contents, 3);
Rewrite ($filename, $rest);
Return ("BOM found, automatically removed._http://www.hzhuti.com/nokia/c6/");
} else {
Return ("BOM found.");
}
}
else return ("BOM not Found.");
}
function rewrite ($filename, $data) {
$filenum = fopen ($filename, "w");
Flock ($filenum, LOCK_EX);
Fwrite ($filenum, $data);
Fclose ($filenum);
}
?>

This is especially handy if we can replace all the contents of all the files in the specified directory as soon as we run.

http://www.bkjia.com/PHPjc/631636.html www.bkjia.com true http://www.bkjia.com/PHPjc/631636.html techarticle to replace the contents of a string we just use PHP-related functions, such as strstr,str_replace, regular expressions, then we have to replace the contents of all files in the directory will need to go through the directory before ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.