php - 如何防止第二个正则表达式重新替换?

原文 标签 php html regex hyperlink

How to prevent of re-replacing by second regex?

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.

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

翻译

我输入时有两个正则表达式,它们是:

// 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);


现在有一个问题(以某种方式发生冲突)。对于常规网址,一切都很好。但是对于基于模式的URL,存在一个问题:第一个正则表达式创建该链接,第二个正则表达式再次创建其href属性值的链接。

我该如何解决?

编辑:根据评论,如何创建单个正则表达式而不是两个正则表达式? (使用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])) {
        // 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;


一种方法是这样做。您将两个表达式与替代字符|合并在一起。然后,在回调函数中,您只需检查是否设置了第三个捕获组(isset($matches[3])),如果是,则第二个正则表达式与字符串匹配,然后替换普通链接,否则替换为link / linktext。

希望您能理解所有内容,并能为您提供帮助。
相关推荐

php - 如何在PHP的$ _SESSION中保存值并进行检索

php - 与特质的定义顺序和内容有关的“未找到特质错误”

php - 按费用和名称对数组进行排序

php - WAMP上的Mysqli,错误-连接尝试失败

php - laravel查询返回奇怪的顺序

php - Silverstripe 3.2-如何在不同选项卡的ModelAdmin中管理同一数据对象的不同列表

php - 在SQL或脚本中排除ID

php - 将多个查询字符串转换为php中的seo友好url

php - Wordpress插件表,尽管sql正确,但无法使用dbDelta创建

php - 在Symfony2中创建服务表格