Php 在X段后插入代码,但避免使用表

Php 在X段后插入代码,但避免使用表,php,regex,explode,Php,Regex,Explode,我想在X段之后插入一些代码,这对于php来说非常简单 public function inject($text, $paragraph = 2) { $exploded = explode("</p>", $text); if (isset($exploded[$paragraph])) { $exploded[$paragraph] = ' MYCODE ' . $exploded[$paragrap

我想在X段之后插入一些代码,这对于php来说非常简单

public function inject($text, $paragraph = 2) {

    $exploded = explode("</p>", $text);
    if (isset($exploded[$paragraph])) {
        $exploded[$paragraph] = '
            MYCODE
            ' . $exploded[$paragraph];

        return implode("</p>", $exploded);
    }
    return $text;
}
公共函数注入($text,$paragration=2){
$spolded=分解(“

”,$text); 如果(isset($分解[$段落]){ $分段[$段落]=' 麦可德 “.$段[$段]; 返回内爆(“

”,美元爆炸); } 返回$text; }
但是,我不想将我的
$text
注入
中,那么如何避免这种情况呢


谢谢编辑:昨晚很晚了

  • PREG\u SPLIT\u DELIM\u CAPTURE
    非常简洁,但我现在添加了一个更好的想法(方法1)。

  • 还改进了方法2,用更快的
    substr

  • 方法1:
    preg\u用
    (*跳过)(*失败)
    (更好)替换回调

    让我们使用对
    inject
    函数的回调直接替换可证明是无表的文本

    这里有一个正则表达式来匹配表自由文本:

    $regex = "~(?si)(?!<table>).*?(?=<table|</table)|<table.*?</table>(*SKIP)(*FAIL)~";
    
    短多了

    下面是$regex的演示向您展示了它如何匹配不包含表的元素

    $text = "<table> to 
    </table>not a table # 1<table> to 
    </table>NOT A TABLE # 2<table> to 
    </table>";
    $regex = "~(?si)(?!<table>).*?(?=<table|</table)|<table.*?</table>(*SKIP)(*FAIL)~";
    $a = preg_match_all($regex,$text,$m);
    print_r($m);
    
    这个
    PREG_SPLIT_DELIM_CAPTURE
    标志的好处是,由于正则表达式模式中的括号,我们捕获的整个分隔符成为数组中的一个元素,因此我们可以隔离表而不会丢失它们。[请参阅底部的演示。]这样,您的字符串将被分成干净的“无桌”和“一张桌子”两部分

    B.步骤2:迭代$tableseparator元素。对于每个元素,执行以下操作:


    if(substr($tableseparator[$i],0,6)=”我有时有点疯狂,有时我喜欢懒惰的模式,但这次我喜欢模糊的模式

    $input = 'test <table><p>wuuut</p><table><p>lolwut</p></table></table> <p>foo bar</p> test1 <p>baz qux</p> test3'; # Some input
    $insertAfter = 2; # Insert after N p tags
    $code = 'CODE'; # The code we want to insert
    
    $regex = <<<'regex'
    ~
    # let's define something
    (?(DEFINE)
       (?P<table>                     # To match nested table tags
          <table\b[^>]*>
             (?:
                (?!</?table\b[^>]*>).
             |
                (?&table)
             )*
          </table\s*>
       )
       (?P<paragraph>                 # To match nested p tags
          <p\b[^>]*>
             (?:
                (?!</?p\b[^>]*>).
             |
                (?&paragraph)
             )*
          </p\s*>
       )
    )
    (?&table)(*SKIP)(*FAIL)           # Let's skip table tags
    |
    (?&paragraph)                     # And match p tags
    ~xsi
    regex;
    
    $output = preg_replace_callback($regex, function($m)use($insertAfter, $code){
        static $counter = 0; # A counter
        $counter++;
        if($counter === $insertAfter){ # Should I explain?
            return $m[0] . $code;
        }else{
            return $m[0];
        }
    }, $input);
    
    var_dump($output); # Let's see what we've got
    
    $input='testwuut

    lolwut

    foo-bar

    test1baz-qux

    test3';一些输入 $insertAfter=2;#在N个p标记后插入 $code='code'#我们要插入的代码
    $regex=不要用regexi解析html如果你看到了最初的简短答案,我对它进行了相当大的扩展,但现在必须走了。@oscar我添加了比昨晚的方法更好的方法1。这个想法唤醒了我,现在看起来Hamza也有类似的想法,使用(*SKIP)(*FAIL)但是看了一眼就不一样了——也许更简洁一些,我现在会详细阅读哈姆扎的想法。非常好!我绝对要试试it@OscarFanelli谢谢你的评论。太疯狂了!!!我醒来时想到了用(*SKIP)(*FAIL)替换preg\u callback,疯狂地工作着,看到强大的哈姆扎之前发布过一个短的。但是答案看起来很不一样,所以现在你有了一个可供选择的代码组合。:@Hamza但我可以投票给你。:)又睡着了。好主意!我一直喜欢你的工作。用(*SKIP)(*FAIL)进行preg_替换_回调的想法把我叫醒,在看到你的答案之前,我做了这件事,并将其添加到我的解决方案中。但它看起来与你的答案大不相同,我现在将详细阅读。@HamZa有一个问题:如果表外没有段落,我会收到一个“没有收到数据”“浏览器出错..@OscarFanelli在我这边。所以我认为问题是elsewhere@OscarFanelli所以没问题了?现在没问题了,但是也许有更好的方法来避免这个问题,编辑正则表达式。但是我对他们不太好:(
    $tableseparator = preg_split( "~(?s)(<table.*?</table>)~", $data, -1, PREG_SPLIT_DELIM_CAPTURE );
    
    $text = "Hi@There@@Oscar@@@@";
    $regex = "~(@+)~";
    $a = preg_split($regex,$text,-1,PREG_SPLIT_DELIM_CAPTURE);
    print_r($a);
    
    $input = 'test <table><p>wuuut</p><table><p>lolwut</p></table></table> <p>foo bar</p> test1 <p>baz qux</p> test3'; # Some input
    $insertAfter = 2; # Insert after N p tags
    $code = 'CODE'; # The code we want to insert
    
    $regex = <<<'regex'
    ~
    # let's define something
    (?(DEFINE)
       (?P<table>                     # To match nested table tags
          <table\b[^>]*>
             (?:
                (?!</?table\b[^>]*>).
             |
                (?&table)
             )*
          </table\s*>
       )
       (?P<paragraph>                 # To match nested p tags
          <p\b[^>]*>
             (?:
                (?!</?p\b[^>]*>).
             |
                (?&paragraph)
             )*
          </p\s*>
       )
    )
    (?&table)(*SKIP)(*FAIL)           # Let's skip table tags
    |
    (?&paragraph)                     # And match p tags
    ~xsi
    regex;
    
    $output = preg_replace_callback($regex, function($m)use($insertAfter, $code){
        static $counter = 0; # A counter
        $counter++;
        if($counter === $insertAfter){ # Should I explain?
            return $m[0] . $code;
        }else{
            return $m[0];
        }
    }, $input);
    
    var_dump($output); # Let's see what we've got