Need help with regular expression. I practice on replacing code in files with the help of regulars. There is a code in the file:
$list = array(); $list[] = '<form method="POST" action="edit"> <a href="title1">Title 1</a> <input type="submit" name="red" value="Edit Page"> <input type="hidden" name="hidden" value="title1"> <input type="hidden" name="prev" value="'.$_SERVER["REQUEST_URI"].'"> </form> <form method="POST" action=""> <input type="hidden" name="hidden_delete" value="title1"> <input type="submit" name="red" value="Delete Page"><hr> </form>'; $list[] = '<form method="POST" action="edit"> <a href="title2">Title 2</a> <input type="submit" name="red" value="Edit Page"> <input type="hidden" name="hidden" value="title2"> <input type="hidden" name="prev" value="'.$_SERVER["REQUEST_URI"].'"> <hr> </form> <form method="POST" action=""> <input type="hidden" name="hidden_delete" value="title2"> <input type="submit" name="red" value="Delete Page"><hr> </form>'; Wrote this replacement:
$link = 'check.php'; $file = file_get_contents($link); $file = preg_replace('~\$list\[\][^<]+\<.*title1[^;]+\;~s', '', $file); file_put_contents($link, $file); What is the essence of the problem. A regular expression removes code from title1 without affecting other code. However, if you put already title2, then both title1 and title2 will be deleted. Please help me fix it to remove only a specific piece of code, that is, either title1 or title2.
preg_replacereplaces entries in the string. Do you understand that this is the wrong approach? - Let's say Pie