Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅匹配regex-PHP中的前两个事件_Php_Regex - Fatal编程技术网

仅匹配regex-PHP中的前两个事件

仅匹配regex-PHP中的前两个事件,php,regex,Php,Regex,我正在尝试用链接替换文本。我现在拥有的正则表达式将替换所有匹配项,但我只想替换前两个匹配项 它应该用链接替换所有出现的find me,除非它位于或标记中 我现在的正则表达式: /<(a|h[1-6])[^>]*>(?:[a-zA-Z0-9\s\'\-\.,]|(?:<(.*)>.*<\/\1>))*<\/(a|h[1-6])>(*SKIP)(*FAIL)|\b(Find Me)\b(?=[^>]*(?:<|$))/ig /]

我正在尝试用链接替换文本。我现在拥有的正则表达式将替换所有匹配项,但我只想替换前两个匹配项

它应该用链接替换所有出现的
find me
,除非它位于或标记中

我现在的正则表达式:

/<(a|h[1-6])[^>]*>(?:[a-zA-Z0-9\s\'\-\.,]|(?:<(.*)>.*<\/\1>))*<\/(a|h[1-6])>(*SKIP)(*FAIL)|\b(Find Me)\b(?=[^>]*(?:<|$))/ig
/]*>(?:[a-zA-Z0-9\s\'-\,]|(?:*)*(*跳过)(*失败)|\b(找到我)\b(?=[^>]*(?:使用未知的
$limit
参数修复了它

$text=preg\u replace($regex,sprintf(“”,'http://example.com“),$text,$limit=2);

Lorem ipsum find me and skip when 
<b>find me</b> is inside a link tag 
like this find me
<h1>find me in the title</h1>
<a href="#">here you can find me too</a>.
<h2 class="heading">Lorem Ipsum dolor find me here too</h2>
<table>
  <tr><td>Cell 1 a</td><td>Find me</td></tr>
  <tr><td>Cell 2 with find me</td><td><a href="#foo">Find me</a></td></tr>
</table>
<h3 class="heading">Duomo Di find me San Martino</h3>
<p>FIND ME as well</p>
$text = 'Lorem ipsum find me and skip when 
    <b>find me</b> is inside a link tag 
    like this find me
    <h1>find me in the title</h1>
    <a href="#">here you can find me too</a>.
    <h2 class="heading">Lorem Ipsum dolor find me here too</h2>
    <table>
      <tr><td>Cell 1 a</td><td>Find me</td></tr>
      <tr><td>Cell 2 with find me</td><td><a href="#foo">Find me</a></td></tr>
    </table>
    <h3 class="heading">Duomo Di find me San Martino</h3>
    <p>FIND ME as well</p>';

$regex = "/<(a|h[1-6])[^>]*>(?:[a-zA-Z0-9\s\'\-\.,]|(?:<(.*)>.*<\/\1>))*<\/(a|h[1-6])>(*SKIP)(*FAIL)|\b(" . preg_quote('find me') . ")\b(?=[^>]*(?:<|$))/i";

$text = preg_replace($regex, sprintf('<a href="%s">$4</a>', 'http://example.com'), $text);