<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. 如何防止被第二个正则表达式重新替换?

        时间:2023-07-15
      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.

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

                  上一篇:PHP - 在文本中查找链接的函数 下一篇:如何使用 Simple-HTML-DOM 提取完整的子链接?

                  相关文章

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