eZ Publish  [4.0]
eztextinputparser.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTextInputParser class
00004 //
00005 // Created on: <17-Jul-2002 13:02:32 bf>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZTextInputParser eztextinputparser.php
00033 
00034 */
00035 
00036 class eZTextInputParser
00037 {
00038     const CHUNK_TEXT = 1;
00039     const CHUNK_TAG = 2;
00040 
00041     /*!
00042 
00043     */
00044     function eZTextInputParser()
00045     {
00046 
00047     }
00048 
00049     /*!
00050      Will parse the input text and create an array of the input.
00051      False will be returned if the parsing
00052     */
00053     function parseText( $text )
00054     {
00055         $returnArray = array();
00056         $pos = 0;
00057 
00058 
00059         while ( $pos < strlen( $text ) )
00060         {
00061             // find the next tag
00062             $tagStart = strpos( $text, "<", $pos );
00063 
00064             if ( $tagStart !== false )
00065             {
00066                 if ( ( $tagStart - $pos ) >= 1 )
00067                 {
00068                     $textChunk = substr( $text, $pos, $tagStart - $pos );
00069                     $pos += $tagStart - $pos;
00070 
00071 
00072                     if ( strlen( trim( $textChunk ) ) != 0 )
00073                     {
00074                         $returnArray[] = array( "Type" => eZTextInputParser::CHUNK_TEXT,
00075                                                 "Text" => $textChunk,
00076                                                 "TagName" => "#text" );
00077 
00078                         eZDebug::writeNotice( $textChunk, "New text chunk in input" );
00079                     }
00080                 }
00081                 // get the tag
00082                 $tagEnd = strpos( $text, ">", $pos );
00083                 $tagChunk = substr( $text, $pos, $tagEnd - $pos + 1 );
00084                 $tagName = preg_replace( "#^<(.+)?(\s.*|>)#m", "\\1", $tagChunk );
00085 
00086                 // check for end tag
00087                 if ( $tagName[0] == "/" )
00088                 {
00089                     print( "endtag" );
00090                 }
00091 
00092                 $returnArray[] = array( "Type" => eZTextInputParser::CHUNK_TAG,
00093                                         "TagName" => $tagName,
00094                                         "Text" => $tagChunk,
00095                                         );
00096 
00097                 $pos += $tagEnd - $pos;
00098                 eZDebug::writeNotice( $tagChunk, "New tag chunk in input" );
00099             }
00100             else
00101             {
00102 
00103                 // just plain text in the rest
00104                 $textChunk = substr( $text, $pos, strlen( $text ) );
00105                 eZDebug::writeNotice( $textChunk, "New text chunk in input" );
00106 
00107                 if ( strlen( trim( $textChunk ) ) != 0 )
00108                 {
00109                     $returnArray[] = array( "Type" => eZTextInputParser::CHUNK_TEXT,
00110                                             "Text" => $textChunk,
00111                                             "TagName" => "#text"  );
00112                 }
00113 
00114                 $pos = strlen( $text );
00115             }
00116 
00117             $pos++;
00118         }
00119         return $returnArray;
00120     }
00121 
00122     /// Contains the tags found
00123     public $TagStack = array();
00124 
00125     /// The tags that don't break the text
00126     public $InlineTags = array( "emphasize", "strong" );
00127 
00128     /// The tags that break the paragraph
00129     public $BreakTags = array();
00130 }
00131 
00132 ?>