Hello everyone, there is a script that reads a sample, searches for the same text in itself, finds and replaces it with an empty line.

Everything works with a bang, only now sometimes some bugs arise, I cannot understand what the problem is, if the script is not touched and not edited everything with a bang, but when, for example, I add some functions to it by hand, (Without touching the search and replace string itself), he refuses to remove information from himself on the model. The code is pretty big, but I'll post it, maybe I missed something.

$variable = $_POST['deleten']; //пост запрос на удаление текста $value = 'deleten'; //значение запроса на удаление $admfile = 'admintoursample.php'; //образец админки $pagefile = 'toursample.php'; //название страницы $contfile = 'content/contentsample.txt'; // контент с названием изображения $contfile2 = 'content/contentsample2.txt'; //контент2 $contback = 'content/contentsample2back.txt'; //backupконтента $contimage = file_get_contents($contfile); //читает название изображения if ($variable == $value) { //если значение совпадает с именем post запроса $admlist = 'adminpan.php'; //админ панель куда записывается $admcont = file_get_contents($admlist); //читаем $admstring = '<p><a href="admintoursample.php">toursample.php</a></p>'; $admcontold = array("$admstring"); //читаем строку из файла страницы если в админке есть такаяже строка заменяем ее на пустую строку "" $admcontnew = array(""); $admphrase = str_replace($admcontold, $admcontnew, $admcont); if(file_put_contents($admlist, $admphrase)) { echo "succes page\n"; } unlink($admfile); //удаляем ненужные файлы , так как удаляем страницу unlink($pagefile); unlink($contfile); unlink($contfile2); unlink($contback); unlink($contimage); echo "Successfuly removed"; $partext = 'resettext.txt'; // текстовый файл с php кодом $parfile = 'reset.php'; // главный файл куда записываются данные после создания страницы , и он их исполняет если понадобится , и при удалении сравнивает текст из 'resettext.txt' и у себя , если есть совпадения удаляет из себя строку которая совпала $parfilecont = file_get_contents($parfile); $partextcont = file_get_contents($partext); $parcontold = array($partextcont); $parcontnew = array(""); $parphrase = str_replace($parcontold, $parcontnew, $parfilecont); if(file_put_contents($parfile, $parphrase)) { unlink($partext); echo "succes reset remove"; } else { echo "/"; } $cretext = 'creatortext.txt'; //тоже самое что выше , только этот скрипт занят другой работой , но смысл один и тотже , поиск замена и удаление. $crefile = 'creator.php'; $crefilecont = file_get_contents($crefile); $cretextcont = file_get_contents($cretext); $crecontold = array($cretextcont); $crecontnew = array(""); $crephrase = str_replace($crecontold, $crecontnew, $crefilecont); if(file_put_contents($crefile, $crephrase)) { unlink($cretext); echo "succes creator remove"; } else { echo "/"; } $selftext = 'deletetext.php'; //тоже самое что и выше. $selffile = 'delete.php'; $selffilecont = file_get_contents($selffile); $selftextcont = file_get_contents($selftext); $selfcontold = array($selftextcont); $selfcontnew = array(""); $selfphrase = str_replace($selfcontold, $selfcontnew, $selffilecont); if(file_put_contents($selffile, $selfphrase)) { unlink($selftext); echo "succes self remove"; } } else { echo "/"; } 
  • First, format the code, and make short comments in the code. - Naumov
  • one
    Honestly, it is not surprising that if you make changes to the Hindu code, something may break. - Firepro
  • right now I will write comments - hovdev
  • When adding, even just echo 'hello', to the code, str_replace stops working, and it refuses to delete something from itself - hovdev
  • Could there be a problem with permissions? I have ubuntu. and pkhp is on the Apache module, when he himself creates files gives them rights only for www, and this file is accessible to everyone, maybe there is some kind of conflict? - hovdev

2 answers 2

As for me, it is easier to refactor and understand something similar than what you currently have. In addition, it takes not so much time, and the pluses are just huge. Go through the lines under the debager to catch the moment when errors occur.

 if ($_POST['deleten'] == 'deleten') { ReplaceContent('adminpan.php', '<p><a href="admintoursample.php">toursample.php</a></p>', true); UnlinkPages(); ReplaceContent('reset.php', 'resettext.txt'); ReplaceContent('creator.php', 'creatortext.txt'); ReplaceContent('delete.php', 'deletetext.php'); } else { echo "/"; } function UnlinkPages() { $unlinkList = [ 'admintoursample.php', 'toursample.php', 'content/contentsample.txt', 'content/contentsample2.txt', 'content/contentsample2back.txt', file_get_contents('content/contentsample.txt') ]; foreach($unlinkList as $link) { unlink($link); } echo "Successfuly removed"; } function ReplaceContent($selffile, $selftext, $noContent = false) { $filecont = file_get_contents($selffile); $textcont = $noContent ? $selftext : file_get_contents($selftext); $phrase = str_replace([$textcont], [""], $filecont); if(file_put_contents($selffile, $phrase)) { unlink($selftext); echo "success " . $selffile; } else { echo "error " . $selffile; } } 
  • Thanks, and what debugger advise? I just haven't used it yet - hovdev
  • @ S1lllver, I usually use the default xDebug, but you may find something more appropriate . - Alex Krass
  • I found the problem, the problem is in apache, because I started the same script, creating files by hand, and even after making changes str_replace works with a bang, it remains to understand why when it creates the file and grants only www the right to edit and edit 'deletetext. php. When editing delete.php, the script refuses to work, although the rights to delete.php are given to everyone - hovdev

The problem is solved, it was in the file extension. renamed deleteexample.txt в ---> deleteexample.php , and when adding code, str_replace works fine, although this file stores only text, very incomprehensible behavior of the function.

Thanks to those who tried to help.