开发者

Use preg_replace

开发者 https://www.devze.com 2023-04-06 08:10 出处:网络
What is the simplest way to change <a href=\"link开发者_JS百科\">title</a> into [url=\"link\"]title[/url]

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号