如何防止用第二个正则表达式重新替换? [英] How to prevent of re-replacing by second regex?
问题描述
我的输入方式有两个正则表达式,这些是:
// //用一个URL替换一个URL链接,就像这样:[LinkName](LinkAddress)
$ str = preg_replace(/ \ [([^] [] *)] \(([^()] *)\) /,< a href ='$ 2'target ='_ blank'> $ 1< / a>,$ str);
//用链接取代常规URL
$ str = preg_replace(/(\ b(?:( ?: https?| ftp):\ / \ / | www\) - A-Z0-9 +功放; @#\ /%=〜_ |:?!;] * [ - A-Z0-9 +功放; @#\ /% = __])/ i,< a href = \$ 1 \target = \_blank\> untitled< / a>,$ str);
现在有一个问题(某种程度上是碰撞)。对于普通的URL,一切都很好。但是对于基于模式的URL,存在一个问题:第一个正则表达式创建一个和第二个正则表达式的链接创建它的 href
-attribute value。
我该如何解决它?
编辑:根据评论,我怎样才能创建一个正则表达式而不是这两个正则表达式? (使用 preg_replace_callback
)。老实说,我尝试过,但它不适用于任何类型的网址。
是否可以组合它们?因为那些输出不一样。第一个有LinkName,第二个有一个常量字符串 untitled
作为它的LinkName。
$ str = preg_replace_callback('/ \ [([^] [] *)] \(([^()] *)\)| (\b(:( ?: HTTPS | FTP):\ / \ / | www\?)[ - A-Z0-9 +功放; @#\ /%=〜_ |?! :,。;] * [ - a-z0-9 +& @#\ /%=〜_ |])/ i',
function($ matches){
if(isset $ matches [3])){
//用一个链接替换一个普通的URL
return< a href ='。$ matches [3]。''target ='_ blank'> untitled< / a>;
} else {
//用一个类似于这种模式的链接替换URL:[LinkName](LinkAddress)
return< a href = $ matches [2]。target ='_ blank'>。matches [1]。< / a>;
}
},$ str);
echo $ str;
一种方法是这样做。您将两个表达式与替代字符 |
合并在一起。然后在你的回调函数中,你只需检查你的第三个捕获组是否被设置( isset($ matches [3])
),如果是,那么你的第二个正则表达式匹配字符串并且你替换一个正常的链接,否则你用link / linktext替换。
我希望你了解一切,我可以帮你。
I have two regex(s) on the way of my input, these:
// replace a URL with a link which is like this pattern: [LinkName](LinkAddress)
$str= preg_replace("/\[([^][]*)]\(([^()]*)\)/", "<a href='$2' target='_blank'>$1</a>", $str);
// replace a regular URL with a link
$str = preg_replace("/(\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|])/i","<a href=\"$1\" target=\"_blank\">untitled</a>", $str);
Now there is a problem (somehow a collision). For regular URLs everything is fine. But for a pattern-based URLs, there is a problem: The first regex create a link of that and second regex again create a link of its href
-attribute value.
How can I fix it?
Edit: According to the comments, how can I create a single regex instead of those two regex? (using preg_replace_callback
). Honestly I tried it but it doesn't work for none kind of URLs ..
Is combining them possible? Because the output of those isn't identical. The first one has a LinkName and the second one has a constant string untitled
as its LinkName.
$str = preg_replace_callback('/\[([^][]*)]\(([^()]*)\)|(\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|])/i',
function($matches) {
if(isset($matches[3])) {
// replace a regular URL with a link
return "<a href='".$matches[3]."' target='_blank'>untitled</a>";
} else {
// replace a URL with a link which is like this pattern: [LinkName](LinkAddress)
return "<a href=".$matches[2]." target='_blank'>".$matches[1]."</a>";
}
}, $str);
echo $str;
One way would be to do it like this. You merge your two expressions together with the alternative character |
. Then in your callback function you just check if your third capture group is set (isset($matches[3])
) and if yes, then your second regular expression matched the string and you replace a normal link, otherwise you replace with link/linktext.
I hope you understand everything and I could help you.
这篇关于如何防止用第二个正则表达式重新替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!