Hello!
There is a line like this:
$string = "<body><div></div><div></div><tr><td></td></tr></body>"; How to insert a character before the last </div> using preg_replace and regular expressions?
The <tr><td></td></tr> section can be any, so this type of brute force does not fit:
$string = "<body><div></div><div></div></body>"; function insertInto($what, $object){ $pattern = '/\<\/div\>\<\/body\>$/i'; $replacement = $what.'${0}'; $newString = preg_replace($pattern, $replacement, $object); return $newString; } $string = insertInto('X', $string); print $string; //Результат: <body><div></div><div>X</div></body>