' ) && strpos( $badStr, '?'.'>' ) > strpos( $badStr, '<'.'?', strpos( $badStr, '?'.'>' ) ) ) { $badStr = substr( $badStr, 0, strpos( $badStr, '<'.'?' ) ) . substr( $badStr, strpos( $badStr, '?'.'>', strpos( $badStr, '?'.'>' ) ) + 2 ); } } //remove comments while( substr_count( $badStr, '' ) && strpos( $badStr, '-->' ) > strpos( $badStr, '' ) ) ) { $badStr = substr( $badStr, 0, strpos( $badStr, '', strpos( $badStr, '-->' ) ) + 3 ); } //now make sure all HTML tags are correctly written (> not in between quotes) for( $x = 0, $goodStr = '', $is_open_tb = false, $is_open_sq = false, $is_open_sq = false; strlen( $chr = $badStr{$x} ); $x++ ) { //take each letter in turn and check if that character is permitted there switch( $chr ) { case '<': if( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 5 ) ) == 'style' ) { $badStr = substr( $badStr, 0, $x ) . substr( $badStr, strpos( strtolower( $badStr ), '', $x ) + 7 ); $chr = ''; } elseif( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 6 ) ) == 'script' ) { $badStr = substr( $badStr, 0, $x ) . substr( $badStr, strpos( strtolower( $badStr ), '', $x ) + 8 ); $chr = ''; } elseif( !$is_open_tb ) { $is_open_tb = true; } else { $chr = '<'; } break; case '>': if( !$is_open_tb || $is_open_dq || $is_open_sq ) { $chr = '>'; } else { $is_open_tb = false; } break; case '"': if( $is_open_tb && !$is_open_dq && !$is_open_sq ) { $is_open_dq = true; } elseif( $is_open_tb && $is_open_dq && !$is_open_sq ) { $is_open_dq = false; } else { $chr = '"'; } break; case "'": if( $is_open_tb && !$is_open_dq && !$is_open_sq ) { $is_open_sq = true; } elseif( $is_open_tb && !$is_open_dq && $is_open_sq ) { $is_open_sq = false; } } $goodStr .= $chr; } //now that the page is valid (I hope) for strip_tags, strip all unwanted tags $goodStr = strip_tags( $goodStr, '