<i id='8VRqt'><tr id='8VRqt'><dt id='8VRqt'><q id='8VRqt'><span id='8VRqt'><b id='8VRqt'><form id='8VRqt'><ins id='8VRqt'></ins><ul id='8VRqt'></ul><sub id='8VRqt'></sub></form><legend id='8VRqt'></legend><bdo id='8VRqt'><pre id='8VRqt'><center id='8VRqt'></center></pre></bdo></b><th id='8VRqt'></th></span></q></dt></tr></i><div id='8VRqt'><tfoot id='8VRqt'></tfoot><dl id='8VRqt'><fieldset id='8VRqt'></fieldset></dl></div>
    • <bdo id='8VRqt'></bdo><ul id='8VRqt'></ul>

    1. <small id='8VRqt'></small><noframes id='8VRqt'>

      <legend id='8VRqt'><style id='8VRqt'><dir id='8VRqt'><q id='8VRqt'></q></dir></style></legend>
    2. <tfoot id='8VRqt'></tfoot>

      1. 如何防止被第二个正则表达式重新替换?

        How to prevent of re-replacing by second regex?(如何防止被第二个正则表达式重新替换?)
      2. <small id='MhNj9'></small><noframes id='MhNj9'>

      3. <i id='MhNj9'><tr id='MhNj9'><dt id='MhNj9'><q id='MhNj9'><span id='MhNj9'><b id='MhNj9'><form id='MhNj9'><ins id='MhNj9'></ins><ul id='MhNj9'></ul><sub id='MhNj9'></sub></form><legend id='MhNj9'></legend><bdo id='MhNj9'><pre id='MhNj9'><center id='MhNj9'></center></pre></bdo></b><th id='MhNj9'></th></span></q></dt></tr></i><div id='MhNj9'><tfoot id='MhNj9'></tfoot><dl id='MhNj9'><fieldset id='MhNj9'></fieldset></dl></div>
        <legend id='MhNj9'><style id='MhNj9'><dir id='MhNj9'><q id='MhNj9'></q></dir></style></legend>
        <tfoot id='MhNj9'></tfoot>
            <bdo id='MhNj9'></bdo><ul id='MhNj9'></ul>

                  <tbody id='MhNj9'></tbody>
                1. 本文介绍了如何防止被第二个正则表达式重新替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的输入有两个正则表达式,这些:

                  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("/((?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|])/i","<a href="$1" target="_blank">untitled</a>", $str);
                  

                  现在有一个问题(不知何故碰撞).对于常规 URL,一切都很好.但是对于基于模式的 URL,存在一个问题:第一个正则表达式创建该链接,第二个正则表达式再次创建其 href 属性值的链接.

                  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.

                  我该如何解决?

                  根据评论,我如何创建一个正则表达式而不是这两个正则表达式?(使用preg_replace_callback).老实说,我试过了,但它不适用于任何类型的 URL ..

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

                  是否可以将它们结合起来?因为它们的输出并不相同.第一个有一个 LinkName,第二个有一个常量字符串 untitled 作为它的 LinkName.

                  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('/[([^][]*)](([^()]*))|((?:(?: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])),如果是,则您的第二个正则表达式与字符串匹配并替换普通链接, 否则替换为链接/链接文本.

                  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.

                  这篇关于如何防止被第二个正则表达式重新替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  <small id='z8skE'></small><noframes id='z8skE'>

                        1. <i id='z8skE'><tr id='z8skE'><dt id='z8skE'><q id='z8skE'><span id='z8skE'><b id='z8skE'><form id='z8skE'><ins id='z8skE'></ins><ul id='z8skE'></ul><sub id='z8skE'></sub></form><legend id='z8skE'></legend><bdo id='z8skE'><pre id='z8skE'><center id='z8skE'></center></pre></bdo></b><th id='z8skE'></th></span></q></dt></tr></i><div id='z8skE'><tfoot id='z8skE'></tfoot><dl id='z8skE'><fieldset id='z8skE'></fieldset></dl></div>
                            <tbody id='z8skE'></tbody>
                          <tfoot id='z8skE'></tfoot><legend id='z8skE'><style id='z8skE'><dir id='z8skE'><q id='z8skE'></q></dir></style></legend>
                          • <bdo id='z8skE'></bdo><ul id='z8skE'></ul>