Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $html = $_GET['html'];
- $lines = preg_split('/[\n]/', $html, -1, PREG_SPLIT_NO_EMPTY);
- foreach ($lines as $key => $value) {
- if (strpos($value, 'div') !== false) {
- //extract div
- preg_match_all('/<div[^>]*>/', $value, $matches);
- if (count($matches[0]) > 0) {
- $div = $matches[0][0];
- //extract id and class attributes
- preg_match_all('/(id\s*=\s*"[^"]+")|(class\s*=\s*"[^"]+")/', $div, $matches2);
- $atr = $matches2[0][0];
- //exact tag name
- preg_match_all('/"[^"]+"/', $atr, $matches3);
- $tag = $matches3[0][0];
- //remove quotes from tag name
- $tag = preg_replace('/"/', '', $tag);
- //replace div with tag name
- $lines[$key] = preg_replace('/div/', $tag, $value);
- //remove id and class
- $lines[$key] = preg_replace('/(id\s*=\s*"[^"]+"\s*)|(class\s*=\s*"[^"]+"\s*)/', '', $lines[$key]);
- //clear excessive spaces at the end
- $lines[$key] = preg_replace('/\s*>/', '>', $lines[$key]);
- //make multiple spaces single
- $lines[$key] = preg_replace('/\s{2,}/', ' ', $lines[$key]);
- } else {
- //extract /div
- preg_match_all('/<\/div>\s*<!--\s*[^--]*-->/', $value, $matches);
- $div = $matches[0][0];
- //extract tag name from comment
- preg_match_all('/(\s*(nav)\s*)|(\s*(main)\s*)|(\s*(header)\s*)|(\s*(section)\s*)|(\s*(article)\s*)|(\s*(aside)\s*)|(\s*(footer)\s*)/', $div, $matches2);
- $tag = trim($matches2[0][0]);
- //replace /div with tag name
- $lines[$key] = preg_replace('/div/', $tag, $value);
- //remove comment tag
- $lines[$key] = preg_replace('/<!--\s*[^-]*-->/', '', $lines[$key]);
- }
- } else {
- continue;
- }
- }
- $result = implode("\n", $lines);
- echo $result;
- //var_dump($lines);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement