Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- global $smilies;
- $smilies = array( ":)" => "glad.png",
- ":(" => "sad.png",
- ":p" => "tongue.png",
- ":P" => "tongue.png",
- ":o" => "surprised.png",
- ":O" => "surprised.png",
- ":d" => "happy.png",
- ":D" => "happy.png",
- ":s" => "confused.png",
- ":S" => "confused.png",
- "B)" => "cool.png",
- "-_-'" => "dissapointed.png",
- ";)" => "wink.png",
- ">_<" => "upset.png",
- ">_<" => "upset.png",
- "^^" => "satisfied.png" );
- global $elements_noparse;
- $elements_noparse = array( "noparse" => "___CONTENT___",
- "code" => "<pre>___CONTENT___</pre>",
- "inl" => "<tt>___CONTENT___</tt>" );
- global $elements_nocontent;
- $elements_nocontent = array( "img" => '<img class="forum-image" src="___ARGUMENT___" alt="___ARGUMENT___" border="0" />' );
- global $elements_noargs;
- $elements_noargs = array( "b" => '<strong>___CONTENT___</strong>',
- "u" => '<span style="text-decoration: underline">___CONTENT___</span>',
- "i" => '<em>___CONTENT___</em>',
- "s" => '<span style="text-decoration: line-through">___CONTENT___</span>',
- "center" => '<div style="text-align: center">___CONTENT___</div>',
- "url" => '<a href="___CONTENT___">___CONTENT___</a>',
- "img" => '<img class="forum-image" src="___CONTENT___" border="0" />',
- "quote" => '<blockquote>___CONTENT___</blockquote>',
- "*" => '<li>___CONTENT___</li>',
- "list" => '<ol>___CONTENT___</ol>',
- "ulist" => '<ul>___CONTENT___</ul>',
- "h1" => '<h2>___CONTENT___</h2>',
- "h2" => '<h3>___CONTENT___</h3>',
- "h3" => '<h4>___CONTENT___</h4>',
- "h4" => '<h5>___CONTENT___</h5>',
- "h5" => '<h6>___CONTENT___</h6>',
- "user" => '<a href="user.php?un=___CONTENT___">___CONTENT___</a>' );
- global $elements_args;
- $elements_args = array( "url" => '<a href="___ARGUMENT___">___CONTENT___</a>',
- "quote" => '<blockquote><span class="quote-user">Posted by ___ARGUMENT___: </span><br/>___CONTENT___</blockquote>',
- "font" => '<span style="font-family: ___ARGUMENT___">___CONTENT___</span>',
- "size" => '<span style="font-size: ___ARGUMENT___">___CONTENT___</span>',
- "color" => '<span style="color: ___ARGUMENT___">___CONTENT___</span>' );
- function FindAllOccurences( $haystack, $needle )
- {
- $toReturn = array();
- $foundPos = 0;
- while ( true )
- {
- $found = stripos( $haystack, $needle, $foundPos + 1 );
- if ( $found === false )
- {
- break;
- }
- $toReturn[] = $foundPos = $found;
- }
- return $toReturn;
- }
- function DoNoparseStuff( $text )
- {
- global $elements_noparse;
- global $npLastFound;
- global $npLastFoundCode;
- $tmpFound = false;
- $tmpCode = false;
- foreach ( $elements_noparse as $code => $format )
- {
- $foundIt = strpos( $text, '[' . $code . ']', $npLastFound );
- if ( $foundIt !== false and ( $tmpFound === false or $tmpFound > $foundIt ) )
- {
- $tmpCode = $code;
- $tmpFound = $foundIt;
- }
- }
- $npLastFound = $tmpFound;
- $npLastFoundCode = $tmpCode;
- }
- function nl2br_custom( $text )
- {
- $text = str_replace( "\r\n", "\n", $text );
- $text = str_replace( "\r", "\n", $text );
- $tokens = explode( "\n", $text );
- $str = '';
- $nsf = 1;
- $c = count( $tokens );
- foreach ( $tokens as $key => $value )
- {
- $str .= $value;
- $trimmed = trim( $value );
- $last3Chars = substr( $trimmed, strlen( $trimmed ) - 3, 3 );
- $last4Chars = substr( $trimmed, strlen( $trimmed ) - 4, 4 );
- $last5Chars = substr( $trimmed, strlen( $trimmed ) - 5, 5 );
- $last6Chars = substr( $trimmed, strlen( $trimmed ) - 6, 6 );
- if ( $nsf != $c and $last3Chars != "/*]" and $last4Chars != "/h1]" and $last4Chars != "/h2]" and $last4Chars != "/h3]" and $last4Chars != "/h4]" and $last4Chars != "/h5]" and $last5Chars != "list]" and $last6Chars != "ulist]" )
- {
- $str .= "<br/>\n";
- }
- else
- {
- $str .= "\n";
- }
- ++$nsf;
- }
- return $str;
- }
- function Parse( $text )
- {
- global $elements_args;
- global $elements_noargs;
- global $elements_nocontent;
- global $elements_noparse;
- global $smilies;
- $foundSome = false;
- foreach ( $elements_noparse as $code => $format )
- {
- if ( strpos( $text, '[' . $code . ']' ) !== false and strpos( $text, '[/' . $code . ']' ) !== false )
- {
- $foundSome = true;
- }
- }
- $sections = array();
- if ( !$foundSome )
- {
- $sections[] = array( $text => true );
- }
- else
- {
- global $npLastFound;
- global $npLastFoundCode;
- $npLastFound = 0;
- $lastSection = 0;
- for ( DoNoparseStuff( $text ); $npLastFound !== false; DoNoparseStuff( $text ) )
- {
- $tag = $npLastFoundCode;
- $tagStart = '[' . $tag . ']';
- $tagEnd = '[/' . $tag . ']';
- $endFound = strpos( $text, $tagEnd, $npLastFound );
- if ( $endFound === false )
- {
- $npLastFound += strlen( $tagStart );
- continue;
- }
- $sections[] = array( substr( $text, $lastSection, $npLastFound - $lastSection ) => true );
- $sections[] = array( substr( $text, $npLastFound, $endFound + strlen( $tagEnd ) - $npLastFound ) => $tag );
- $lastSection = $endFound + strlen( $tagEnd );
- $npLastFound += strlen( $tagStart );
- }
- $sections[] = array( substr( $text, $lastSection, strlen( $text ) - $lastSection ) => true );
- }
- $parsedText = "";
- foreach ( $sections as $key => $sectionInfo )
- {
- $theKeyList = array_keys( $sectionInfo );
- $toParseOriginal = $theKeyList[ 0 ];
- $toParse = htmlentities( $toParseOriginal );
- if ( $sectionInfo[ $toParseOriginal ] !== true )
- {
- $format = $elements_noparse[ $sectionInfo[ $toParseOriginal ] ];
- $tag = $sectionInfo[ $toParseOriginal ];
- if ( $tag !== 'code' and $tag != 'inl' )
- {
- $toParse = nl2br_custom( $toParse );
- }
- $stuff = $toParse;
- $stuff = str_replace( '[' . $tag . ']', '', $stuff );
- $stuff = str_replace( '[/' . $tag . ']', '', $stuff );
- $parsed = str_replace( '___CONTENT___', $stuff, $format );
- $parsedText .= $parsed;
- continue;
- }
- $toParse = trim( $toParse );
- $toParse = nl2br_custom( $toParse );
- foreach ( $smilies as $smily => $image )
- {
- for ( $lastFound = strpos( $toParse, $smily ); $lastFound !== false; $lastFound = @strpos( $toParse, $smily, $lastFound + 1 ) )
- {
- $contents = '<img class="smily" src="res/' . $image . '" alt="' . htmlentities( $smily ) . '" />';
- $replaceEnd = $lastFound + strlen( $smily );
- $stuff = substr( $toParse, $lastFound, $replaceEnd - $lastFound );
- $toParse = str_replace( $stuff, $contents, $toParse );
- $lastFound = $lastFound + strlen( $contents );
- }
- }
- foreach ( $elements_noargs as $code => $format )
- {
- $codeStart = '[' . $code . ']';
- $codeEnd = '[/' . $code . ']';
- for ( $lastFound = strpos( $toParse, $codeStart ); $lastFound !== false; $lastFound = @strpos( $toParse, $codeStart, $lastFound + 1 ) )
- {
- $endFound = strpos( $toParse, $codeEnd, $lastFound );
- if ( $endFound === false )
- {
- continue;
- }
- $contentsStart = $lastFound + strlen( $codeStart );
- $contents = substr( $toParse, $contentsStart, $endFound - $contentsStart );
- $guts = str_replace( "___CONTENT___", $contents, $format ); // Couldn't think of a name, ok? :P
- $replaceEnd = $endFound + strlen( $codeEnd );
- $stuff = substr( $toParse, $lastFound, $replaceEnd - $lastFound );
- $toParse = str_replace( $stuff, $guts, $toParse );
- $lastFound = $lastFound + strlen( $guts );
- }
- }
- foreach ( $elements_args as $code => $format )
- {
- $codeStart = '[' . $code . '';
- $codeEnd = '[/' . $code . ']';
- for ( $lastFound = strpos( $toParse, $codeStart ); $lastFound !== false; $lastFound = @strpos( $toParse, $codeStart, $lastFound + 1 ) )
- {
- if ( $toParse[ $lastFound + strlen( $codeStart ) ] !== '=' )
- {
- continue;
- }
- $elemEnd = strpos( $toParse, "]", $lastFound );
- if ( $elemEnd === false )
- {
- return false;
- }
- $endFound = strpos( $toParse, $codeEnd, $lastFound );
- if ( $endFound === false )
- {
- continue;
- }
- $argStart = $lastFound + strlen( $codeStart );
- $theArg = substr( $toParse, $argStart + 1, $elemEnd - $argStart - 1 );
- $contentsStart = $argStart + 1 + strlen( $theArg ) + 1;
- $contents = substr( $toParse, $contentsStart, $endFound - $contentsStart );
- $guts = str_replace( "___CONTENT___", $contents, $format ); // Couldn't think of a name, ok? :P
- $guts = str_replace( "___ARGUMENT___", $theArg, $guts );
- $replaceEnd = $endFound + strlen( $codeEnd );
- $stuff = substr( $toParse, $lastFound, $replaceEnd - $lastFound );
- $toParse = str_replace( $stuff, $guts, $toParse );
- $lastFound = $lastFound + strlen( $guts );
- }
- }
- foreach ( $elements_nocontent as $code => $format )
- {
- $codeStart = '[' . $code . '';
- for ( $lastFound = strpos( $toParse, $codeStart ); $lastFound !== false; $lastFound = @strpos( $toParse, $codeStart, $lastFound + 1 ) )
- {
- if ( $toParse[ $lastFound + strlen( $codeStart ) ] !== '=' )
- {
- continue;
- }
- $elemEnd = strpos( $toParse, "]", $lastFound );
- if ( $elemEnd === false )
- {
- return false;
- }
- $argStart = $lastFound + strlen( $codeStart );
- $theArg = substr( $toParse, $argStart + 1, $elemEnd - $argStart - 1 );
- $guts = str_replace( "___ARGUMENT___", $theArg, $format );
- $replaceEnd = $elemEnd + 1;
- $stuff = substr( $toParse, $lastFound, $replaceEnd - $lastFound );
- $toParse = str_replace( $stuff, $guts, $toParse );
- $lastFound = $lastFound + strlen( $guts );
- }
- }
- $parsedText .= $toParse;
- }
- return $parsedText;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement