I need to read the string $mysting and replace all incident of e.x.
<img src="dummypic.jpg alt="dummy pic">
with
<img src="dummypic.jpg alt="dummy pic" title="dummy pic">
in other words add the title where it is missing, and make the title tag have the same value as the alt tag.
some incident my have additional parameters like border, with, height - and these should be left untouched ...
Rather than a regex, have a look into PHP DOMDocument to modify attributes, especially if there will be any complexity.
You could use phpQuery or QueryPath to do that:
Though it might be feasible in this simple case to resort to an regex:
(It would make more sense to use preg_replace_callback to ensure no title= attribute is present yet.)
My answer is a link to a well known answer!
RegEx match open tags except XHTML self-contained tags
You really want to do this using an html parser instead of regexes. Regexes are not suited for html processing.
I usually write quick dirty hacks in these types of situations (usually working with structured data, that might have inconsistencies such as templating engine tags, conditionals, etc.). IMO its a terrible solution, but I can bang out these scripts super fast.
Eg.