将em标签转换为链接

时间:2014-01-23 11:50:36

标签: php preg-replace php-5.3

标题清晰。

如何转换这篇迷你文章

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>.

2 个答案:

答案 0 :(得分:0)

这是jquery以防万一:

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

答案 1 :(得分:0)

试试这个:

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