oxstrmb.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxStrMb
00007 {
00013     protected $_sEncoding = 'UTF-8';
00014 
00020     protected $_aUmls = array( "\xc3\xa4", "\xc3\xb6", "\xc3\xbc", "\xC3\x84", "\xC3\x96", "\xC3\x9C", "\xC3\x9F" );
00021 
00026     protected $_aUmlEntities = array('&auml;', '&ouml;', '&uuml;', '&Auml;', '&Ouml;', '&Uuml;', '&szlig;' );
00027 
00033     public function __construct()
00034     {
00035     }
00036 
00044     public function strlen($sStr)
00045     {
00046         return mb_strlen($sStr, $this->_sEncoding);
00047     }
00048 
00058     public function substr( $sStr, $iStart, $iLength = null )
00059     {
00060         $iLength = is_null( $iLength ) ? $this->strlen( $sStr ) : $iLength;
00061         return mb_substr( $sStr, $iStart, $iLength, $this->_sEncoding );
00062     }
00063 
00073     public function strpos( $sHaystack, $sNeedle, $iOffset = null )
00074     {
00075         $iPos = false;
00076         if ( $sHaystack && $sNeedle ) {
00077             $iOffset = is_null( $iOffset ) ? 0 : $iOffset;
00078             $iPos = mb_strpos( $sHaystack, $sNeedle, $iOffset, $this->_sEncoding );
00079         }
00080         return $iPos;
00081     }
00082 
00091     public function strstr( $sHaystack, $sNeedle )
00092     {
00093         // additional check according to bug in PHP 5.2.0 version
00094         if ( !$sHaystack ) {
00095             return false;
00096         }
00097         return mb_strstr($sHaystack, $sNeedle, false, $this->_sEncoding);
00098     }
00099 
00107     public function strtolower($sString)
00108     {
00109         return mb_strtolower($sString, $this->_sEncoding);
00110     }
00111 
00119     public function strtoupper($sString)
00120     {
00121         return mb_strtoupper($sString, $this->_sEncoding);
00122     }
00123 
00131     public function htmlspecialchars($sString)
00132     {
00133         return htmlspecialchars($sString, ENT_QUOTES, $this->_sEncoding);
00134     }
00135 
00143     public function htmlentities($sString)
00144     {
00145         return htmlentities($sString, ENT_QUOTES, $this->_sEncoding);
00146     }
00147 
00155     public function html_entity_decode($sString)
00156     {
00157         return html_entity_decode( $sString, ENT_QUOTES, $this->_sEncoding );
00158     }
00159 
00170     public function preg_split($sPattern, $sString, $iLimit = -1, $iFlag = 0)
00171     {
00172         return preg_split( $sPattern.'u', $sString, $iLimit, $iFlag );
00173     }
00174 
00186     public function preg_replace($aPattern, $sString, $sSubject, $iLimit = -1, $iCount = null)
00187     {
00188         if ( is_array($aPattern) ) {
00189             foreach ( $aPattern as &$sPattern) {
00190                 $sPattern = $sPattern.'u';
00191             }
00192         } else {
00193             $aPattern = $aPattern.'u';
00194         }
00195         return preg_replace( $aPattern, $sString, $sSubject, $iLimit, $iCount);
00196     }
00197 
00209     public function preg_match($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
00210     {
00211         return preg_match( $sPattern.'u', $sSubject, $aMatches, $iFlags, $iOffset);
00212     }
00213 
00225     public function preg_match_all($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
00226     {
00227         return preg_match_all( $sPattern.'u', $sSubject, $aMatches, $iFlags, $iOffset);
00228     }
00229 
00237     public function ucfirst($sSubject)
00238     {
00239         $sString = $this->strtoupper($this->substr($sSubject, 0, 1));
00240         return $sString . $this->substr($sSubject, 1);
00241     }
00242 
00253     public function wordwrap( $sString, $iLength = 75, $sBreak = "\n", $blCut = null )
00254     {
00255         if ( !$blCut ) {
00256             $sRegexp = "/^(.{1,{$iLength}}\r?(\s|$|\n)|.{1,{$iLength}}[^\r\s\n]*\r?(\n|\s|$))/u";
00257         } else {
00258             $sRegexp = "/^([^\s]{{$iLength}}|.{1,{$iLength}}\s)/u";
00259         }
00260 
00261         $iStrLen = mb_strlen( $sString, $this->_sEncoding );
00262         $iWraps = floor( $iStrLen / $iLength );
00263 
00264         $i = $iWraps;
00265         $sReturn = '';
00266         $aMatches = array();
00267         while ( $i > 0 ) {
00268             $iWraps = floor( mb_strlen( $sString, $this->_sEncoding ) / $iLength );
00269 
00270             $i = $iWraps;
00271             if ( preg_match( $sRegexp, $sString, $aMatches ) ) {
00272                 $sStr = $aMatches[0];
00273                 $sReturn .= preg_replace( '/\s$/s', '', $sStr ) . $sBreak;
00274                 $sString = $this->substr( $sString, mb_strlen( $sStr, $this->_sEncoding ) );
00275             } else {
00276                 break;
00277             }
00278             $i--;
00279         }
00280         $sReturn = preg_replace( "/$sBreak$/", '', $sReturn );
00281         if ($sString) {
00282             $sReturn .= $sBreak.$sString;
00283         }
00284         return $sReturn;
00285     }
00286 
00299     public function recodeEntities( $sInput, $blToHtmlEntities = false, $aUmls = array(), $aUmlEntities = array() )
00300     {
00301         $aUmls = ( count( $aUmls ) > 0 ) ? array_merge( $this->_aUmls, $aUmls) : $this->_aUmls;
00302         $aUmlEntities = ( count( $aUmlEntities ) > 0 ) ? array_merge( $this->_aUmlEntities, $aUmlEntities) : $this->_aUmlEntities;
00303         return $blToHtmlEntities ? str_replace( $aUmls, $aUmlEntities, $sInput ) : str_replace( $aUmlEntities, $aUmls, $sInput );
00304     }
00305 
00313     public function hasSpecialChars( $sStr )
00314     {
00315         return $this->preg_match( "/(".implode( "|", $this->_aUmls  )."|(&amp;))/", $sStr );
00316     }
00317 
00327     public function cleanStr( $sStr, $sCleanChr = ' ' )
00328     {
00329         return $this->preg_replace( "/\n|\r|\t|\xc2\x95|\xc2\xa0|;/", $sCleanChr, $sStr );
00330     }
00331 
00339     public function jsonEncode($data)
00340     {
00341         return json_encode($data);
00342     }
00343 
00352     public function strip_tags( $sString, $sAllowableTags = '' )
00353     {
00354         if ( stripos( $sAllowableTags, '<style>' ) === false ) {
00355             // strip style tags with definitions within
00356             $sString = $this->preg_replace( "'<style[^>]*>.*</style>'siU", '', $sString );
00357         }
00358         return strip_tags( $sString, $sAllowableTags );
00359     }
00360 
00370     public function strrcmp( $sStr1, $sStr2 )
00371     {
00372         return -strcmp( $sStr1, $sStr2 );
00373     }
00374 }