Advertisement
Guest User

Untitled

a guest
May 27th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. function betweenTag($html, $tag = 'pre'){
  2. $replace = array();
  3. $j = 0;
  4. do{
  5. $new = false;
  6. //Поиск открывающего тэга (одного!)
  7. preg_match('%(<'.$tag.'[^>]*>)(.*)%s', $html, $m);
  8.  
  9. if(isset($m[1], $m[2])){
  10. //Начинаем поиски закрывающих тегов (всех до конца документа)
  11. preg_match_all('%</'.$tag.'[^>]*>%is', $m[2], $tmp, PREG_OFFSET_CAPTURE);
  12. if( ! empty($tmp[0])){
  13. foreach($tmp[0] as $j=>$subTmp){
  14. $closeTag = $subTmp[0]; //закрывающий тэг
  15. $subText = substr($m[2], 0, $subTmp[1]); //Тексту внутри тэгов
  16.  
  17. //подсчет открывающих тэгов внутри полученного текста
  18. preg_match_all('%(<'.$tag.'[^>]*>)%s', $subText, $count);
  19. if(count($count[0])==$j){
  20. $replace[] = array($m[1], $subText, $closeTag);
  21. $new = true;
  22. break;
  23. }
  24. }
  25. $html = substr($m[2], $tmp[0][$j][1] + strlen($tmp[0][$j][0]));
  26. }
  27. if( ! $new){
  28. if(isset($tmp[0][$j]) && $j < $count[0]){
  29. $subTmp = $tmp[0][$j];
  30. $closeTag = $subTmp[0];
  31.  
  32. $subText = substr($m[2], 0, $subTmp[1]).$closeTag;
  33. $html = substr($m[2], $subTmp[1] + strlen($closeTag));
  34. $replace[] = array($m[1], $subText, $closeTag);
  35. }else{
  36. $replace[] = array($m[1], $m[2], '');
  37. $html = '';
  38. }
  39. }
  40. }else{
  41. $html = '';
  42. }
  43. }while( ! empty($html));
  44. return $replace;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement