Sterge in fisier linie care contine cuvant din baza de date

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
Stefan
Mesaje: 117

Sterge in fisier linie care contine cuvant din baza de date

Salut, se poate cauta intr-un fisier un nume dintr-o baza de date ? Iar daca il gaseste sa stearga linia respectiva

MarPlo Mesaje: 4343
Salut
Poti sa folosesti functia containsWord() din acest exemplu ca sa verifici data un cuvant exista intr-un sir:

Cod: Selectaţi tot

//returns True if the $word exists in $str, otherwise, False
function containsWord($str, $word){
  return !!preg_match('#\b' . preg_quote($word, '#') . '\b#i', $str);
}

$str ='String to test if the word "check" exists in this string';
$word ='Check';
if(containsWord($str, $word)) echo 'word found';
else echo 'word not in string';
Iar cu file_get_contents() poti sa preiei intr-un sir continutul unui fisier:

Cod: Selectaţi tot

$str = file_get_contents('adresa/fisier.ext');
Nu stiu ce linie sa stearga, din fisier sau din tabelul mysql.
Daca e din tabelul mysql, cand faci Select-ul pt cuvant, iei si id-ul lui din tabel (daca are coloana pt id), apoi se aplica Delete cu acel id.

Cod: Selectaţi tot

DELETE FROM nume_tabel WHERE id = $id

Stefan Mesaje: 117
Sa stearga linia pe care se afla cuantul respectiv din fisier

MarPlo Mesaje: 4343
Te poti folosi de acest cod:

Cod: Selectaţi tot

$file_r ='file_read.txt';  //address of file to read
$file_s ='file_save.txt';  //address of file to save after delete the lines (can be the same $file_r)
$del_w ='word';  //word by which to delete the lines

//get the $file_r content into an array, by lines
$data_r = file($file_r, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$nrl = count($data_r);  //number of lines

//traverses the lines and store in $data_s the lines without $del_w
$data_s =[];
for($i=0; $i<$nrl; $i++){
  if(!preg_match('#\b' . preg_quote($del_w, '#') . '\b#i', $data_r[$i])) $data_s[] = $data_r[$i];
}

//saves the lines in $file_s
if(file_put_contents($file_s, implode(PHP_EOL, $data_s))) echo 'Content saved';
else echo 'Unable to save data in: '. $file_s;

Subiecte similare