/** * gets attribute from a tag string */ public static function fixHtmlTagStructure(&$string, $remove_surrounding_p_tags = 1) { if (empty($string)) { return; } // Combine duplicate <p> tags RLText::combinePTags($string); // Move div nested inside <p> tags outside of it RLText::moveDivBlocksOutsidePBlocks($string); // Remove duplicate ending </p> tags RLText::removeDuplicateTags($string, '/p'); if ($remove_surrounding_p_tags) { // Remove surrounding <p></p> blocks RLText::removeSurroundingPBlocks($string); } }
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']; RLText::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; }