2014-01-23 57 views
1

标题清楚。将em标签转换为链接

如何我这个小文章

Test article here. This is a <em>test test</em> article. 
This is the other <em>line</em>. 

转换成由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>. 
+0

“href”的值是从哪里来的? – dachi

+0

http://stackoverflow.com/questions/2139140/replace-non-html-links-with-a-tags?rq=1 –

+0

这是来自em标签 – Emre

回答

0

试试这个:

$code = preg_replace('!<em>(.+?)</em>!', '<a href="http://stackoverflow.com/\1">\1</a>', $code); 
+0

内部的文本,工作得很好,谢谢 – Emre

0

这是jQuery的以防万一:

$("em").wrap("<a href='http://stackoverflow.com/"+$("em").html()+"'></a>"); 
$("em").contents().unwrap();