I wanted to remove a certain piece of code in a php file, I decided to do it with str_replace (). In this way:

$filename = 'check.php'; $a = 'public function actionTitle1() { $this->title = "Title 1"; $this->meta_author = "Arandar"; $this->meta_copyright = "Copyright © 2018 Arandar. All Rights Reserved."; $content = $this->view->render("title1", array(), true); $this->render($content); }'; $b = ''; $file1 = file_get_contents($filename); $file1 = str_replace($a, $b, $file1); file_put_contents($filename, $file1); 

This is how the file looks like, where you need to delete the file in compliance with tabs and hyphenations:

  public function actionTitle1() { $this->title = "Title 1"; $this->meta_author = "Arandar"; $this->meta_copyright = "Copyright © 2018 Arandar. All Rights Reserved."; $content = $this->view->render("title1", array(), true); $this->render($content); } 

But alas, does not work. The file is updated, but remains unchanged. Tell me, please, what is the mistake, because I’ve already been in everything, I found some options on JS, but I would like to use PHP.

  • And there is no option for replacing without taking into account tabulation at least, not to mention the new lines already? - Arandar
  • I haven’t been able to work with regular ones yet.) Just you understand, I completely copied the code from the main file to a clean one, didn’t change anything and everything works there, but basically the file is just updated, but it doesn’t want to work, no matter how I follow the tabulation , line breaks and everything else .. That's what confuses me - Arandar
  • one
    What is your goal? Why did you need to delete the PCP code in another PCP file? I assume that your task can be solved differently, without any changes at all. - Edward
  • I have articles on the site, I decided to make the "delete" button, so that everything happens automatically, and not go to the files and clean everything there every time. Deleting the article itself from the folder is a simple matter, but it was harder to delete the code. On the Internet, there is only information on how to remove a code from a specific line by number, but I can’t predict on which line I’ll have to delete the code next time. Now I’m trying to replace a specific code with an empty value, but in the future, instead of the name, there will be just variables within which the values ​​will be written. - Arandar
  • I am still a novice, so this is the only normal option that came to my mind. But when he did not work, he decided to turn to more senior comrades) - Arandar

1 answer 1

For your current code ( actionTitle() controller), the following template is actionTitle() :

 '~public function actionTitle1\(.*?\)[^{]+\{[^}]+\}~s' 

As you can see from the beginning of the template, a line that begins with the words public function actionTitle1 and ends with a curly bracket } falls under the deletion. But if in the body of the method there is another pair of curly braces, then this option will not work correctly. That is why I said in the comments that for regular expressions you need to consider all possible variants of substrings.

 $str = ' public function actionTitle1() { $this->title = "Title 1"; $this->meta_author = "Arandar"; $this->meta_copyright = "Copyright © 2018 Arandar. All Rights Reserved."; $content = $this->view->render("title1", array(), true); $this->render($content); } public function actionBody() { $this->title = "Title 1"; $this->meta_author = "Arandar"; $this->meta_copyright = "Copyright © 2018 Arandar. All Rights Reserved."; $content = $this->view->render("title1", array(), true); $this->render($content); } '; echo preg_replace('~public function actionTitle1\(.*?\)[^{]+\{[^}]+\}~s', '', $str); 

As a result, you get the line:

  public function actionBody() { $this->title = "Title 1"; $this->meta_author = "Arandar"; $this->meta_copyright = "Copyright © 2018 Arandar. All Rights Reserved."; $content = $this->view->render("title1", array(), true); $this->render($content); } 
  • Thanks again! It helped a lot) - Arandar