[英]Replace a href tag by a shortcode structure with preg_replace
I'm trying to replace a href and I have the following code : <a href="http://www.mysite.com" target="_blank">My site</a>
我正在尝试替换href,并且输入以下代码:
<a href="http://www.mysite.com" target="_blank">My site</a>
with that : 接着就,随即 :
$textarea_content = '<a href="http://www.mysite.com" target="_blank">My site</a>';
$patterns = array(
"/<a href=/",
"/target*\=*\'*\_blank*\'*\>/",
"/<*\/a>/"
);
$replacements = array(
"[url=",
"]",
"[/url]"
);
$textarea_content = preg_replace($patterns,$replacements, $textarea_content);
I need to convert the link tag to this structure : 我需要将链接标记转换为以下结构:
[url='http://www.mysite.com']My site[/url]
The problem is that the output is something like : <a href="" http:="" www.mysite.com''="" target="_blank">My site</a>
问题在于输出结果类似于:
<a href="" http:="" www.mysite.com''="" target="_blank">My site</a>
What I'm doing wrong ? 我做错了什么? Thanks for help!
感谢帮助!
echo preg_replace('/(?:\<a\s+.*href=\"([^\"]*)"[^\>]*\>([^\<]*)\<\/a\>)/', '[url=\'\\1\']\\2[/url]', $text);
Assuming: 假设:
$text = 'Bla bla bla <a href="http://www.mysite.com" target="_blank">My site</a> bla bla bla';
Result: 结果:
Bla bla bla [url='http://www.mysite.com']My site[/url] bla bla bla
EDIT According to OP's comment below: 编辑根据以下OP的评论:
If want to get back your HTML you just have to do this: 如果要取回HTML,则只需执行以下操作:
echo preg_replace('/(?:\[url=\'([^\']*)\'\]([^\[]*)\[\/url\])/', '<a href="\\1" target="_blank">\\2</a>', $parsed);
Assuming: 假设:
$parsed = 'Bla bla bla [url=\'http://www.mysite.com\']My site[/url] bla bla bla';
Result: 结果:
Bla bla bla <a href="http://www.mysite.com" target="_blank">My site</a> bla bla bla
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.