Created
March 9, 2012 02:46
-
-
Save AstDerek/2004740 to your computer and use it in GitHub Desktop.
Basic div pair highlighter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$contents = htmlspecialchars(@$_POST['contents']); | |
if (!$contents) { ?> | |
<form method="post"> | |
<textarea name="contents"></textarea> | |
<br> | |
<input type="submit"> | |
</form> | |
<?php | |
exit; | |
} | |
$processed = ''; | |
$divs = array(); | |
$total = 0; | |
$current = 0; | |
while (preg_match('@(</?div)@i',$contents,$match)) { | |
$div = $match[1]; | |
if ($div{4} == '/') { | |
$level = count($divs); | |
$current = array_pop($divs); | |
} | |
else { | |
$total++; | |
$current = $total; | |
$divs []= $current; | |
$level = count($divs); | |
} | |
$new = str_replace('div','<span class="div-encloser div-pair-' . $current . ' div-level-' . $level . '">div</span>',$div); | |
$contents = preg_replace('@' . preg_quote($div) . '@',$new,$contents,1); | |
} | |
?> | |
<script type="text/javascript" src="js/jquery-1.4.3.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
$('span').bind('click',function(){ | |
var $this = $(this), | |
classes = $this.attr('class'), | |
pair = classes.replace(/.*?(div-pair-[^ ]*).*/ig,'$1'), | |
level = classes.replace(/.*?(div-level-[^ ]*).*/ig,'$1'); | |
$('.div-encloser').removeClass('active-pair').removeClass('active-level'); | |
$('.' + pair).addClass('active-pair'); | |
$('.' + level).addClass('active-level'); | |
}) | |
}); | |
</script> | |
<style type="text/css"> | |
span | |
{font-weight:bold;} | |
.active-pair | |
{color:#f33;} | |
.active-level | |
{background-color:#ccc;} | |
</style> | |
<pre><?php echo $contents ?></pre> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment