Try this:
$code = preg_replace('!<em>(.+?)</em>!', '<a href="http://stackoverflow.com/\1">\1</a>', $code);
Question
The title is clear.
How can I convert this mini article
Test article here. This is a <em>test test</em> article.
This is the other <em>line</em>.
to that by php
Test article here. This is a <a href="http://stackoverflow.com/test test">test test</a> article.
This is the other <a href="http://stackoverflow.com/line">line</a>.
Solution
Try this:
$code = preg_replace('!<em>(.+?)</em>!', '<a href="http://stackoverflow.com/\1">\1</a>', $code);
OTHER TIPS
this is in jquery just in case:
$("em").wrap("<a href='http://stackoverflow.com/"+$("em").html()+"'></a>");
$("em").contents().unwrap();