What is the simplest way to change
<a href="link开发者_JS百科">title</a>
into
[url="link"]title[/url]
Thanks
Offering improved version:
1) will consider both '" as argument enclosures
2) will work with multiple tags
3) allows for other params in the a tag and various spacing
<?php
$r = "<a href=\"test.html\" arg2=\"foo\" bar3=\"_something_else\">test</a>\n
<a href= \"test2.html\" >test2</a>\n
<a href = 'test3.html'>test3</a>\n
";
$r = preg_replace("/<a.*?href[^=]*=[^'\"]*['\"]([^'\"]+)['\"].*?>([^<]+)<\/a>/", "[url=\\1]\\2[/url]", $r);
echo $r;
output:
[url=test.html]test[/url]
[url=test2.html]test2[/url]
[url=test3.html]test3[/url]
You can try with
$result = preg_replace('/<a href="(.*)">(.*)<\/a>/', '[url="\1"]\2[/url]', $input);
Try using this Expression: #<a href="(.*?)">(.*?)</a>#s
.
Example:
$str = '<a href="link">title</a>';
$str = preg_replace( '#<a href="(.*?)">(.*?)</a>#s', '[url="\\1"]\\2[/url]', $str);
Demo: Codepad
精彩评论