|
eZ Publish
[trunk]
|
00001 <?php 00002 /** 00003 * File containing the eZTextInputParser class. 00004 * 00005 * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. 00006 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 00007 * @version //autogentag// 00008 * @package kernel 00009 */ 00010 00011 /*! 00012 \class eZTextInputParser eztextinputparser.php 00013 00014 */ 00015 00016 class eZTextInputParser 00017 { 00018 const CHUNK_TEXT = 1; 00019 const CHUNK_TAG = 2; 00020 00021 /*! 00022 00023 */ 00024 function eZTextInputParser() 00025 { 00026 00027 } 00028 00029 /*! 00030 Will parse the input text and create an array of the input. 00031 False will be returned if the parsing 00032 */ 00033 function parseText( $text ) 00034 { 00035 $returnArray = array(); 00036 $pos = 0; 00037 00038 00039 while ( $pos < strlen( $text ) ) 00040 { 00041 // find the next tag 00042 $tagStart = strpos( $text, "<", $pos ); 00043 00044 if ( $tagStart !== false ) 00045 { 00046 if ( ( $tagStart - $pos ) >= 1 ) 00047 { 00048 $textChunk = substr( $text, $pos, $tagStart - $pos ); 00049 $pos += $tagStart - $pos; 00050 00051 00052 if ( strlen( trim( $textChunk ) ) != 0 ) 00053 { 00054 $returnArray[] = array( "Type" => eZTextInputParser::CHUNK_TEXT, 00055 "Text" => $textChunk, 00056 "TagName" => "#text" ); 00057 00058 eZDebug::writeNotice( $textChunk, "New text chunk in input" ); 00059 } 00060 } 00061 // get the tag 00062 $tagEnd = strpos( $text, ">", $pos ); 00063 $tagChunk = substr( $text, $pos, $tagEnd - $pos + 1 ); 00064 $tagName = preg_replace( "#^<(.+)?(\s.*|>)#m", "\\1", $tagChunk ); 00065 00066 // check for end tag 00067 if ( $tagName[0] == "/" ) 00068 { 00069 print( "endtag" ); 00070 } 00071 00072 $returnArray[] = array( "Type" => eZTextInputParser::CHUNK_TAG, 00073 "TagName" => $tagName, 00074 "Text" => $tagChunk, 00075 ); 00076 00077 $pos += $tagEnd - $pos; 00078 eZDebug::writeNotice( $tagChunk, "New tag chunk in input" ); 00079 } 00080 else 00081 { 00082 00083 // just plain text in the rest 00084 $textChunk = substr( $text, $pos, strlen( $text ) ); 00085 eZDebug::writeNotice( $textChunk, "New text chunk in input" ); 00086 00087 if ( strlen( trim( $textChunk ) ) != 0 ) 00088 { 00089 $returnArray[] = array( "Type" => eZTextInputParser::CHUNK_TEXT, 00090 "Text" => $textChunk, 00091 "TagName" => "#text" ); 00092 } 00093 00094 $pos = strlen( $text ); 00095 } 00096 00097 $pos++; 00098 } 00099 return $returnArray; 00100 } 00101 00102 /// Contains the tags found 00103 public $TagStack = array(); 00104 00105 /// The tags that don't break the text 00106 public $InlineTags = array( "emphasize", "strong" ); 00107 00108 /// The tags that break the paragraph 00109 public $BreakTags = array(); 00110 } 00111 00112 ?>