class_editor_rte.php

来自「sabreipb 2.1.6 utf-8中文版本!」· PHP 代码 · 共 1,216 行 · 第 1/3 页

PHP
1,216
字号
	*/	function _parse_anchor_tag( $tag, $between_text, $opening_tag, $parse_tag='' )	{		$mytag = 'url';		$href  = $this->_get_value_of_option( 'href', $opening_tag );				$href  = str_replace( '<', '&lt;', $href );		$href  = str_replace( '>', '&gt;', $href );		$href  = str_replace( ' ', '%20' , $href );				if ( preg_match( '#^mailto\:#is', $href ) )		{			$mytag = 'email';			$href  = str_replace( "mailto:", "", $href );		}				return "[$mytag=\"$href\"]".$this->_recurse_and_parse( $tag, $between_text, '_parse_anchor_tag', $parse_tag )."[/$mytag]";	}		/*-------------------------------------------------------------------------*/	// Recursively parse tags	/*-------------------------------------------------------------------------*/		/**	* RTE: Recursively parse tags	*	* @param	string	Tag	* @param	string	Text between opening and closing tag	* @param	string	Callback Function	* @param	string	Parse tag	* @return	string	Converted text	*/	function _recurse_and_parse( $tag, $text, $function, $parse_tag='' )	{		//-----------------------------------------		// INIT		//-----------------------------------------				$tag              = strtolower($tag);		$open_tag         = "<".$tag;		$open_tag_len     = strlen($open_tag);		$close_tag        = "</".$tag.">";		$close_tag_len    = strlen($close_tag);		$start_search_pos = 0;		$tag_begin_loc    = 1;				//-----------------------------------------		// Start the loop		//-----------------------------------------				while ( $tag_begin_loc !== FALSE )		{			$lowtext       = strtolower($text);			$tag_begin_loc = @strpos($lowtext, $open_tag, $start_search_pos);			$lentext       = strlen($text);			$quoted        = '';			$got           = FALSE;			$tag_end_loc   = FALSE;						//-----------------------------------------			// No opening tag? Break			//-----------------------------------------					if ( $tag_begin_loc === FALSE )			{				break;			}						//-----------------------------------------			// Pick through text looking for delims			//-----------------------------------------						for ( $end_opt = $tag_begin_loc; $end_opt <= $lentext; $end_opt++ )			{				$chr = $text{$end_opt};								//-----------------------------------------				// We're now in a quote				//-----------------------------------------								if ( ( in_array( $chr, $this->delimiters ) ) AND $quoted == '' )				{					$quoted = $chr;				}								//-----------------------------------------				// We're not in a quote any more				//-----------------------------------------								else if ( ( in_array( $chr, $this->delimiters ) ) AND $quoted == $chr )				{					$quoted = '';				}								//-----------------------------------------				// Found the closing bracket of the open tag				//-----------------------------------------								else if ( $chr == '>' AND ! $quoted )				{					$got = TRUE;					break;				}								else if ( ( in_array( $chr, $this->non_delimiters ) ) AND ! $tag_end_loc )				{					$tag_end_loc = $end_opt;				}			}						//-----------------------------------------			// Not got the complete tag?			//-----------------------------------------						if ( ! $got )			{				break;			}						//-----------------------------------------			// Not got a tag end location?			//-----------------------------------------						if ( ! $tag_end_loc )			{				$tag_end_loc = $end_opt;			}						//-----------------------------------------			// Extract tag options...			//-----------------------------------------						$tag_opts        = substr( $text   , $tag_begin_loc + $open_tag_len, $end_opt - ($tag_begin_loc + $open_tag_len) );			$actual_tag_name = substr( $lowtext, $tag_begin_loc + 1            , ( $tag_end_loc - $tag_begin_loc ) - 1 );						//-----------------------------------------			// Check against actual tag name...			//-----------------------------------------						if ( $actual_tag_name != $tag )			{				$start_search_pos = $end_opt;				continue;			}				//-----------------------------------------			// Now find the end tag location			//-----------------------------------------						$tag_end_loc = strpos( $lowtext, $close_tag, $end_opt );						//-----------------------------------------			// Not got one? Break!			//-----------------------------------------						if ( $tag_end_loc === FALSE )			{				break;			}				//-----------------------------------------			// Check for nested tags			//-----------------------------------------						$nest_open_pos = strpos($lowtext, $open_tag, $end_opt);						while ( $nest_open_pos !== FALSE AND $tag_end_loc !== FALSE )			{				//-----------------------------------------				// It's not actually nested				//-----------------------------------------								if ( $nest_open_pos > $tag_end_loc )				{					break;				}								if ( $this->debug == 2)				{					print "\n\n<hr>( ".htmlspecialchars($open_tag)." ) NEST FOUND</hr>\n\n";				}								$tag_end_loc   = strpos($lowtext, $close_tag, $tag_end_loc   + $close_tag_len);				$nest_open_pos = strpos($lowtext, $open_tag , $nest_open_pos + $open_tag_len );			}						//-----------------------------------------			// Make sure we have an end location			//-----------------------------------------						if ( $tag_end_loc === FALSE )			{				$start_search_pos = $end_opt;				continue;			}				$this_text_begin  = $end_opt + 1;			$between_text     = substr($text, $this_text_begin, $tag_end_loc - $this_text_begin);			$offset           = $tag_end_loc + $close_tag_len - $tag_begin_loc;						//-----------------------------------------			// Pass to function			//-----------------------------------------						$final_text       = $this->$function($tag, $between_text, $tag_opts, $parse_tag);						//-----------------------------------------			// #DEBUG			//-----------------------------------------						if ( $this->debug == 2)			{				print "<hr><b>REPLACED {$function}($tag, ..., $tag_opts):</b><br />".htmlspecialchars(substr($text, $tag_begin_loc, $offset))."<br /><b>WITH:</b><br />".htmlspecialchars($final_text)."<hr>NEXT ITERATION";			}							//-----------------------------------------			// Swap text			//-----------------------------------------						$text             = substr_replace($text, $final_text, $tag_begin_loc, $offset);			$start_search_pos = $tag_begin_loc + strlen($final_text);		} 			return $text;	}	/**	* RTE: parse recursively: Quick method	*	* @param	string	Tag	* @param	string	Text between opening and closing tag	* @param	string	Callback Function	* @param	string	Parse tag	* @return	string	Converted text	*/	function _recurse_and_parse_quick( $tag, $text, $function, $parse_tag='' )	{		//-----------------------------------------		// Set up		//-----------------------------------------				$tag       = strtolower( $tag );		$open_tag  = "<".$tag;		$close_tag = "</".$tag.">";		$lowtext   = strtolower( $text );				//-----------------------------------------		// Return if no content or no opening tag		//-----------------------------------------				if ( ( ! $text ) OR ( ! strstr( $lowtext, $open_tag ) ) )		{			return $text;		}				//-----------------------------------------		// Go loopy		//-----------------------------------------				while( strstr( $lowtext, $open_tag ) !== FALSE )		{			//-----------------------------------------			// Quick check			//-----------------------------------------						if ( ! strstr( $lowtext, $open_tag ) )			{				break;			}						if ( ! strstr( $lowtext, $close_tag ) )			{				break;			}						//-----------------------------------------			// Try and get a match. First tag to the			// very last tag			//-----------------------------------------						preg_match( "#($open_tag(?:.+?)?".">)(.+?)$close_tag#is", $text, $match );						if ( $this->debug == 2 )			{				print "<hr><pre>";				print_r( array_map('htmlspecialchars',$match) );				print "</pre></hr>";			}						$entire_match = $match[0];			$opening_tag  = $match[1];			$between_text = $match[2];						if ( ! $entire_match )			{				break;			}						//-----------------------------------------			// Look for nested tags			//-----------------------------------------						if ( strstr( strtolower( $between_text ), $open_tag ) !== FALSE )			{				if ( $this->debug == 2)				{					print "\n\n<hr>( ".htmlspecialchars($open_tag)." ) NEST FOUND</hr>\n\n";				}								//-----------------------------------------				// How many opening tags?				//-----------------------------------------								$open_tag_count  = substr_count( $between_text, $open_tag );				$close_tag_count = substr_count( $between_text, $close_tag );				$chopped_text    = $entire_match;				$tmp_text        = preg_replace( "#(^|.+?)".preg_quote( $entire_match, '#' )."#s", '', $text );								//-----------------------------------------				// Okay, pick through the text finding as				// many closing tags as we have opening tags				//-----------------------------------------								while( ( $open_tag_count != $close_tag_count ) OR $tmp_text != '' )				{					preg_match( "#(.+?)$close_tag#is", $tmp_text, $match );										if ( $this->debug == 2)					{						print "<hr>IN RECURSE TMP TEXT: ".htmlspecialchars($tmp_text)."<br>Open tags: {$open_tag_count}<br />Closed tags: {$close_tag_count}<hr>";					}								if ( $match[1] )					{						$open_tag_count  += intval( substr_count( $match[1], $open_tag ) );						$close_tag_count++;												$chopped_text .= $match[0];						$tmp_text      = preg_replace( "#(^|.+?)".preg_quote( $match[0], '#' )."#s", '', $tmp_text );					}					else					{						// Cannot find, somethings wrong so break						break;					}				}								//-----------------------------------------				// Piece back together the 'between_text'				//-----------------------------------------								preg_match( "#$open_tag(?:.+?)?".">(.*)$close_tag#is", $chopped_text, $newmatch );								$between_text = $newmatch[1];				$entire_match = $chopped_text;			}						//-----------------------------------------			// Pass to handler			//-----------------------------------------						if ( $between_text )			{				$newtext = $this->$function( $tag, $between_text, $opening_tag, $parse_tag );				//$text    = preg_replace( "#".preg_quote( $entire_match, '#' )."#", $newtext, $text );				$text    = str_replace( $entire_match, $newtext, $text );								if ( $this->debug == 2)				{					print "<hr><b>REPLACED:</b><br />".htmlspecialchars($entire_match)."<br /><b>WITH:</b><br />".htmlspecialchars($newtext)."<br /><b>TEXT IS NOW</b>:<br />".htmlspecialchars($text)."<hr>";				}			}		}				if ( $this->debug == 2)		{			print "<hr>NEW TEXT: ".htmlspecialchars($text)."\n\n<br /><span style='color:red'>ALL DONE - RETURNING \$text now....</span><hr>";		}					return $text;	}		/*-------------------------------------------------------------------------*/	// Get value of option	/*-------------------------------------------------------------------------*/		/**	* RTE: Extract option HTML	*	* @param	string	Option	* @param	string	Text	* @return	string	Converted text	*/	function _get_value_of_option( $option, $text )	{		preg_match( "#$option(\s+?)?\=(\s+?)?[\"']?(.+?)([\"']|(\s+?)|$|>)#is", $text, $matches );		//preg_match( "#$option(\s+?)?\=(\s+?)?[\"']?(.+?)([\"']|$|>|\s)#is", $text, $matches );		return trim( $matches[3] );	}	}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?