Exemplo n.º 1
0
 /**
  * gets attribute from a tag string
  */
 public static function fixHtmlTagStructure(&$string, $remove_surrounding_p_tags = 1)
 {
     if (empty($string)) {
         return;
     }
     // Combine duplicate <p> tags
     NNText::combinePTags($string);
     // Move div nested inside <p> tags outside of it
     NNText::moveDivBlocksOutsidePBlocks($string);
     // Remove duplicate ending </p> tags
     NNText::removeDuplicateTags($string, '/p');
     if ($remove_surrounding_p_tags) {
         // Remove surrounding <p></p> blocks
         NNText::removeSurroundingPBlocks($string);
     }
 }
Exemplo n.º 2
0
 public static function cleanSurroundingTags($tags, $elements = array('p', 'span'))
 {
     require_once __DIR__ . '/text.php';
     $breaks = '(?:(?:<br ?/?>|:\\|:)\\s*)*';
     $keys = array_keys($tags);
     $string = implode(':|:', $tags);
     // Remove empty tags
     while (preg_match('#<(' . implode('|', $elements) . ')(?: [^>]*)?>\\s*(' . $breaks . ')<\\/\\1>\\s*#s', $string, $match)) {
         $string = str_replace($match['0'], $match['2'], $string);
     }
     // Remove paragraphs around block elements
     $block_elements = array('p', 'div', 'table', 'tr', 'td', 'thead', 'tfoot', 'h[1-6]');
     $block_elements = '(' . implode('|', $block_elements) . ')';
     while (preg_match('#(<p(?: [^>]*)?>)(\\s*' . $breaks . ')(<' . $block_elements . '(?: [^>]*)?>)#s', $string, $match)) {
         if ($match['4'] == 'p') {
             $match['3'] = $match['1'] . $match['3'];
             NNText::combinePTags($match['3']);
         }
         $string = str_replace($match['0'], $match['2'] . $match['3'], $string);
     }
     while (preg_match('#(</' . $block_elements . '>\\s*' . $breaks . ')</p>#s', $string, $match)) {
         $string = str_replace($match['0'], $match['1'], $string);
     }
     $tags = explode(':|:', $string);
     $new_tags = array();
     foreach ($tags as $key => $val) {
         $key = isset($keys[$key]) ? $keys[$key] : $key;
         $new_tags[$key] = $val;
     }
     return $new_tags;
 }