update($file,$i,$msg); instead of this: $data=$this->rea"/> update($file,$i,$msg); instead of this: $data=$this->rea">
Home >Backend Development >PHP Tutorial >Construction project construction contract sample text PHP Chinese text operation class
Let me give you a simple text operation class
I wrote it before, but I have never had the chance to use it. Text is not as good as a database
Data is saved in lines and ends with n. Please note that the data you enter must end with "n" , these are the most basic class members. For text, special attention should be paid to efficiency issues. For example, if you want to update lines 2, 3, and 4 of the file, use:
for($i=2;$i<=4;$i++ )$this->update($file,$i,$msg); is not as good as this:
$data=$this->read($file);
for($i=2;$i<=4 ;$i++)$data[$i]=$msg;
$this->write($file,$data);
class text{
var $key=""; //Data protection
var $keylen; //Data protection length
function text() //Constructor of the class
{
$this->keylen=strlen($this->lkeylen); //Get the protection length
}
function read($file) //Read the file and return an array
{
if(file_exists($file)):
$data=@file($file);
$data[0]=substr($ data[0],$this->keylen);
return $a;
endif;
}
function write($file,$data=array()) //Write file, data is an array
{
if ($fp=fopen($file,"w")):
flock($fp,3);
fwrite($fp,$this->key);
foeach($data as $v)fwrite($ fp,$v);
return true;
endif;
}
function add($file,$msg)
//Add data to the end of the file (create the file first if it does not exist)
{
$a=$this- >read($file); //This does not matter whether the file exists or not
$a[]=$msg;
return $this->write($file,$a);
}
function insert($file,$ n,$msg) //Insert data before $nth
{
if($a=$this->read($file)):
$a[$n]=$msg.$a[$n ];
return true;
endif;
}
function updata($file,$n,$msg) //Update $n data
{
if($a=$this->read($file)):
$a[$n]=$msg;
return true;
endif
}
function del($file,$n) //Delete $n data
{
return $this->updata($file,$ n,"");
}
}
The above has introduced the PHP Chinese text manipulation classes for the construction project construction contract demonstration text, including the content of the construction project construction contract demonstration text. I hope it will be helpful to friends who are interested in PHP tutorials.