Virajsinh

find text and replace in file using php

Jul 17th, 2021 (edited)
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.55 KB | None | 0 0
  1. <?php
  2.     $filename = "path/file.php";
  3.     $code_add = file_get_contents(__DIR__."/views/code.php");
  4.     $string_to_replace = "<?php if(get_option('allow_registration') == 1) { ?>";
  5.     $replace_with = "<?php if(get_option('allow_registration') == 1) { ?>\n".$code_add;
  6.     $content = file_get_contents($filename);
  7.     $content_chunks = explode($string_to_replace, $content);
  8.     $content = implode($replace_with, $content_chunks);
  9.     file_put_contents($filename, $content);
  10.  
  11.     $css_add = "end of the line add";
  12.     file_put_contents($filename, $css_add . PHP_EOL, FILE_APPEND);
  13. ?>
Add Comment
Please, Sign In to add comment