I'm trying to create a function in PHP that would search in a string for all a href occurences and if title is not set it should replace it with the text value between > text </a>
I don't know what is the best way to do it, thinking about something like:
$s = preg_replace('/< a[^>]*?href=[\'"](.*?)[\'"][^>]*?title=[\'"](.*?)[\'"][^>]*?>(.*?)<\/a>/si','< a href="$1" title="$2">$3</a>',$s);
How can I check in the regex to see if $2 is set and if it isn't replace it with $3, also $3 can be something like img src="..." alt="..." and in this case I would like to get the value of alt.
First of all I would like to know if this can be done in PHP and how, but any help would be apreciated.