Embed code after paragraph X, but avoid tables

I would like to add some code after X paragraphs, and this is pretty easy with 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;
}

But I do not want to enter $textinside <table>, so how to avoid this?

thank

+4
source share
2 answers

I'm sometimes a little crazy, sometimes I go for patterns that are lazy, but this time I'm going for something obscure.

$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 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 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 see what we've got

- - -- php

Literature:

+3
source

: .

  • PREG_SPLIT_DELIM_CAPTURE , ( 1).

  • 2, strstr substr

1: preg_replace_callback (*SKIP)(*FAIL) ()

, , , , inject.

:

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

, , -, .

:

$injectedString = preg_replace_callback($regex,
        function($m){return inject($text,$m[0]);},
            $data);

!

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

: Array ( [0] => Array ( [0] => not a table # 1 [1] => NOT A TABLE # 2 ) )

, html $data , . , .

2

, .

, preg_split PREG_SPLIT_DELIM_CAPTURE.

, , preg_split, , .

. 1: split $data : : <table </table>

, , (?s)<table.*?</table>

, <table, .

, -

$tableseparator = preg_split( "~(?s)(<table.*?</table>)~", $data, -1, PREG_SPLIT_DELIM_CAPTURE );

PREG_SPLIT_DELIM_CAPTURE , , , , , . [. .] , " " "-".

. 2: $tablesparator.

if(substr($tableseparator[$i],0,6)=="<table")

<table, ( ). , , inject() .

. 3: $tableseparator ( , inject).

, , preg_split, !

, , , , .:)

preg_split PREG_SPLIT_DELIM_CAPTURE

, preg_split:

$text = "Hi@There@@Oscar@@@@";
$regex = "~(@+)~";
$a = preg_split($regex,$text,-1,PREG_SPLIT_DELIM_CAPTURE);
print_r($a);

: Array ( [0] => Hi [1] => @ [2] => There [3] => @@ [4] => Oscar [5] => @@@@ [6] => )

, ( @)? , , , .

+2

Source: https://habr.com/ru/post/1537262/


All Articles