Dideved - 7:44 pm on Jun 14, 2013 (gmt 0)
@stipko If this page is being generated by your own CMS, then by far your best option would be to insert string A during your normal template rendering process.
If for whatever reason that isn't an option, and you truly have no other choice but to parse and manipulate your own HTML, then your best bet is the DOMDocument class (http://php.net/manual/en/class.domdocument.php). Even the very best regular expression will have trouble accounting for all the possible variations that can occur in HTML. The DOMDocument class, on the other hand, uses a real HTML parser. Use the loadHTML method (http://www.php.net/manual/en/domdocument.loadhtml.php), then you can use normal DOM methods, such as getElementsByTagName, getElementById, childNodes, nextSibling, and appendChild and insertBefore.