×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
0
Language: PHP
Posted by: Norman Liebich
Added: Feb 26, 2015 12:01 PM
Modified: Mar 5, 2015 1:40 PM
Views: 1863
Tags: explode html str
  1. /**
  2. * @param $delimiter string splitting token like "--page break--"
  3. * @param $html string the html code to split
  4. * return array
  5. **/
  6. function split_html($delimiter, $html)
  7. {
  8.     $out = explode($delimiter, $html);
  9.    
  10.     $numParts = count($out);
  11.     for($i=0; $i<$numParts; $i++) {
  12.         $unclosedTags = get_unclosed_tags($out[$i]);
  13.         if (!empty($unclosedTags)) {
  14.             foreach($unclosedTags as $tag) {
  15.                 $out[$i] .= "</{$tag}>";
  16.            
  17.                 if ($numParts > $i+1) {
  18.                     $out[$i+1] = "<{$tag}>".$out[$i+1];
  19.                 }
  20.             }
  21.         }
  22.     }
  23.     return $out;
  24. }
  25. /**
  26. * Helper function, return the unclosed tag-names of $html in order of their appearance
  27. **/
  28. function get_unclosed_tags($html)
  29. {
  30.     $open=array();
  31.     $close=array();
  32.     preg_match_all("/<([^\/> ]+)[^>\/]*>/", $html, $open);
  33.     preg_match_all("/<\/([^>]+)>/", $html, $close);
  34.  
  35.     $closed = $close[1];
  36.  
  37.     $stillOpen = array_filter($open[1], function($a) use (&$closed) {
  38.         $i=array_search($a, $closed);
  39.         if ($i!==false) {
  40.             unset($closed[$i]);
  41.             return false;
  42.         }
  43.         return true;
  44.     });
  45.     return $stillOpen;
  46. }