All pastes #798955 Raw Edit

Untitled

public php v1 · immutable
#798955 ·published 2007-11-27 04:28 UTC
rendered paste body
<?php /* vim: set ts=4 sw=4 noet encoding=utf-8 : */	/*	 * Copyright 2007 Lucien "LiNuCe" Nardini, linuce gmail com	 *	 * This file is free software; you can redistribute it and/or	 * modify it under the terms of the GNU General Public License as	 * published by the Free Software Foundation; either version 2 of	 * the License, or (at your option) any later version.	 *	 * This file is distributed in the hope that it will be useful,	 * but WITHOUT ANY WARRANTY; without even the implied warranty of	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	 * GNU General Public License for more details.	 */	function str_ctrl_clean( $s )	{		$iLen = strlen( $s );		$iByte = 0 ;		for( $i=0 ; $i<$iLen ; $i++ )		{			$iByte = ord( $s[$i] );			if( ( $iByte < 32 )||( $iByte == 127 ) )			{				$s[$i] = ' ' ;			}		}		return trim($s) ;	}	function str_clean( $s, $iLen )	{		if( strcasecmp( mb_detect_encoding( $s, 'UTF-8', TRUE ), 'UTF-8' ) == 0 )		{			return mb_substr( str_ctrl_clean($s), 0, $iLen, 'UTF-8' );		}		return null ;	}	/* string slipounet( string $sMessage )	 *	 * This function parses <b>, <u>, <i>, <s> and <tt> HTML tags and 	 * converts URLs to <a> tags in $sMessage. It returns a string where 	 * useless or wrong tags are htmlentities()'ed. It is rock-solid	 * because I'm an expert in titanium and adamantium.	 *	 * NOTE : it also vonvert &lt; &gt; and &amp; to <, > and &. 	 */	function slipounet( $sMessage, $iLen )	{		$sMessage = str_clean( $sMessage, $iLen );		if( $sMessage == null )		{			die( 'Invalid message: ' . $sMessage );		}		$sMessage = preg_replace( '/<[ ]*(\/?)[ ]*(b|i|s|tt|u)[ ]*>/i', ' <\1\2> ', $sMessage );		$sMessage = str_replace(  '&lt;', chr(27) .  'lt' . chr(27), $sMessage );		$sMessage = str_replace(  '&gt;', chr(27) .  'gt' . chr(27), $sMessage );		$sMessage = str_replace( '&amp;', chr(27) . 'amp' . chr(27), $sMessage );		$aWords = explode( ' ', $sMessage );		$iMax   = count( $aWords );		$aTags  = array();		for( $i=0 ; $i<$iMax ; $i++ )		{			$sWord = $aWords[$i];			if( preg_match( '/<(b|i|s|tt|u)>/i', $sWord ) )			{				$sOpenTag = strtolower( substr( $sWord, 1, strlen($sWord)-2 ) );				array_push( $aTags, $sOpenTag );				$aWords[$i] = strtolower($sWord);				continue ;			}			if( preg_match( '/<\/(b|i|s|tt|u)>/i', $sWord ) )			{				$sOpenTag = array_pop( $aTags );				$sCloseTag = strtolower( substr( $sWord, 2, strlen($sWord)-3 ) );				if( $sOpenTag )				{					if( $sOpenTag==$sCloseTag )					{						$aWords[$i] = strtolower($sWord) ;					}					else					{						$aWords[$i] = chr(27) . $sCloseTag . chr(27) ;						array_push( $aTags, $sOpenTag );					}				}				else				{					$aWords[$i] = chr(27) . $sCloseTag . chr(27) ;				}				continue ;			}			if((( strncmp( $sWord, 'http://', 7 )  == 0 )&&( strlen($sWord) > 7 ))			 ||(( strncmp( $sWord, 'https://', 8 ) == 0 )&&( strlen($sWord) > 8 ))			 ||(( strncmp( $sWord, 'ftp://', 6 )   == 0 )&&( strlen($sWord) > 6 )))			{				$aWords[$i] = '<a href="' . htmlspecialchars($sWord) . '">[url]</a>' ;				continue ;			}					$aWords[$i] = htmlspecialchars( $sWord );		}		$sMessage = implode( ' ', $aWords );		while( $sTag = array_pop( $aTags ) )		{			$sMessage = $sMessage . '</' . $sTag . '>' ;		}		$sMessage = preg_replace( '/ <(\/?)(b|i|s|tt|u)> /i', '<\1\2>', $sMessage );		$sMessage = preg_replace( '/ ' . chr(27) . '(b|i|s|tt|u)' . chr(27) . ' /i', '&lt;/\1&gt;', $sMessage );		$sMessage = str_replace( chr(27) . 'lt' . chr(27), '&lt;', $sMessage );		$sMessage = str_replace( chr(27) . 'gt' . chr(27), '&gt;', $sMessage );		$sMessage = str_replace( chr(27) . 'amp' . chr(27), '&amp;', $sMessage );		return $sMessage ;	}?>