regex - Need to split a string with line breaks by finding PHP tags -


i'm trying split string line breaks finding php tags.

here code have far:

$contents = ' test more test test 1 <?php  test 2 , test 4 test 6 ?> test 7 test 9 <?php  test 10 test 12 >? test 13 <?php test 14 test 16  ?> test 17 '; 

as can tell, php code test examples, , odd test examples outside php tags.

what looking extract array every iteration of php code:

expected result:

array(     [0] =>  <?php              test 2 , test 4             test 6             ?>      [1] =>  <?php              test 10             test 12             >?      [2] =>  <?php test 14             test 16              ?> ) 

i have tried preg_split end tags, , capturing $explode[1] beginning tags code wrong...

$ends = preg_split("/[?>]/s", $contents, preg_split_no_empty, preg_split_delim_capture ); print_r($ends); foreach($ends $flufcode){   $trimcode = explode('<?php', $flufcode);   echo $trimcode . " next:"; } 

so far preg_split not work, believe regex not scanning after line breaks.

your example code wrong. , wrong expected result... anyway. , regex parsing code <?php echo '?>'; ?> failed.

for , easy parsing should use token_get_all. example you.

$tokens = token_get_all($contents);  $catch = false; $codes = array(); $index = 0; foreach ($tokens $token)     {     if (is_array($token) && $token[0] == \t_open_tag)         {         $catch = true;         $index++;         $codes[$index] = '';         }     if ($catch)         $codes[$index] .= is_array($token) ? $token[1] : $token;      if (is_array($token) && $token[0] == \t_close_tag)         {         $catch = false;         }     }  var_export($codes); 

will produce provided data.

array (   1 => '<?php test 2 , test 4 test 6 ?> ',   2 => '<?php test 10 test 12 >? test 13 <?php test 14 test 16 ?> ', ) 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -