oxstrmb.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxStrMb
00007 {
00008 
00014     protected $_sEncoding = 'UTF-8';
00015 
00021     protected $_aUmls = array("\xc3\xa4", "\xc3\xb6", "\xc3\xbc", "\xC3\x84", "\xC3\x96", "\xC3\x9C", "\xC3\x9F");
00022 
00028     protected $_aUmlEntities = array('&auml;', '&ouml;', '&uuml;', '&Auml;', '&Ouml;', '&Uuml;', '&szlig;');
00029 
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 
00062         return mb_substr($sStr, $iStart, $iLength, $this->_sEncoding);
00063     }
00064 
00074     public function strpos($sHaystack, $sNeedle, $iOffset = null)
00075     {
00076         $iPos = false;
00077         if ($sHaystack && $sNeedle) {
00078             $iOffset = is_null($iOffset) ? 0 : $iOffset;
00079             $iPos = mb_strpos($sHaystack, $sNeedle, $iOffset, $this->_sEncoding);
00080         }
00081 
00082         return $iPos;
00083     }
00084 
00093     public function strstr($sHaystack, $sNeedle)
00094     {
00095         // additional check according to bug in PHP 5.2.0 version
00096         if (!$sHaystack) {
00097             return false;
00098         }
00099 
00100         return mb_strstr($sHaystack, $sNeedle, false, $this->_sEncoding);
00101     }
00102 
00110     public function strtolower($sString)
00111     {
00112         return mb_strtolower($sString, $this->_sEncoding);
00113     }
00114 
00122     public function strtoupper($sString)
00123     {
00124         return mb_strtoupper($sString, $this->_sEncoding);
00125     }
00126 
00135     public function htmlspecialchars($sString, $iQuotStyle = ENT_QUOTES)
00136     {
00137         return htmlspecialchars($sString, $iQuotStyle, $this->_sEncoding);
00138     }
00139 
00148     public function htmlentities($sString, $iQuotStyle = ENT_QUOTES)
00149     {
00150         return htmlentities($sString, $iQuotStyle, $this->_sEncoding);
00151     }
00152 
00161     public function html_entity_decode($sString, $iQuotStyle = ENT_QUOTES)
00162     {
00163         return html_entity_decode($sString, $iQuotStyle, $this->_sEncoding);
00164     }
00165 
00176     public function preg_split($sPattern, $sString, $iLimit = -1, $iFlag = 0)
00177     {
00178         return preg_split($sPattern . 'u', $sString, $iLimit, $iFlag);
00179     }
00180 
00192     public function preg_replace($aPattern, $sString, $sSubject, $iLimit = -1, $iCount = null)
00193     {
00194         if (is_array($aPattern)) {
00195             foreach ($aPattern as &$sPattern) {
00196                 $sPattern = $sPattern . 'u';
00197             }
00198         } else {
00199             $aPattern = $aPattern . 'u';
00200         }
00201 
00202         return preg_replace($aPattern, $sString, $sSubject, $iLimit, $iCount);
00203     }
00204 
00216     public function preg_match($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
00217     {
00218         return preg_match($sPattern . 'u', $sSubject, $aMatches, $iFlags, $iOffset);
00219     }
00220 
00232     public function preg_match_all($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
00233     {
00234         return preg_match_all($sPattern . 'u', $sSubject, $aMatches, $iFlags, $iOffset);
00235     }
00236 
00244     public function ucfirst($sSubject)
00245     {
00246         $sString = $this->strtoupper($this->substr($sSubject, 0, 1));
00247 
00248         return $sString . $this->substr($sSubject, 1);
00249     }
00250 
00261     public function wordwrap($sString, $iLength = 75, $sBreak = "\n", $blCut = null)
00262     {
00263         if (!$blCut) {
00264             $sRegexp = "/^(.{1,{$iLength}}\r?(\s|$|\n)|.{1,{$iLength}}[^\r\s\n]*\r?(\n|\s|$))/u";
00265         } else {
00266             $sRegexp = "/^([^\s]{{$iLength}}|.{1,{$iLength}}\s)/u";
00267         }
00268 
00269         $iStrLen = mb_strlen($sString, $this->_sEncoding);
00270         $iWraps = floor($iStrLen / $iLength);
00271 
00272         $i = $iWraps;
00273         $sReturn = '';
00274         $aMatches = array();
00275         while ($i > 0) {
00276             $iWraps = floor(mb_strlen($sString, $this->_sEncoding) / $iLength);
00277 
00278             $i = $iWraps;
00279             if (preg_match($sRegexp, $sString, $aMatches)) {
00280                 $sStr = $aMatches[0];
00281                 $sReturn .= preg_replace('/\s$/s', '', $sStr) . $sBreak;
00282                 $sString = $this->substr($sString, mb_strlen($sStr, $this->_sEncoding));
00283             } else {
00284                 break;
00285             }
00286             $i--;
00287         }
00288         $sReturn = preg_replace("/$sBreak$/", '', $sReturn);
00289         if ($sString) {
00290             $sReturn .= $sBreak . $sString;
00291         }
00292 
00293         return $sReturn;
00294     }
00295 
00308     public function recodeEntities($sInput, $blToHtmlEntities = false, $aUmls = array(), $aUmlEntities = array())
00309     {
00310         $aUmls = (count($aUmls) > 0) ? array_merge($this->_aUmls, $aUmls) : $this->_aUmls;
00311         $aUmlEntities = (count($aUmlEntities) > 0) ? array_merge($this->_aUmlEntities, $aUmlEntities) : $this->_aUmlEntities;
00312 
00313         return $blToHtmlEntities ? str_replace($aUmls, $aUmlEntities, $sInput) : str_replace($aUmlEntities, $aUmls, $sInput);
00314     }
00315 
00323     public function hasSpecialChars($sStr)
00324     {
00325         return $this->preg_match("/(" . implode("|", $this->_aUmls) . "|(&amp;))/", $sStr);
00326     }
00327 
00337     public function cleanStr($sStr, $sCleanChr = ' ')
00338     {
00339         return $this->preg_replace("/\n|\r|\t|\xc2\x95|\xc2\xa0|;/", $sCleanChr, $sStr);
00340     }
00341 
00349     public function jsonEncode($data)
00350     {
00351         return json_encode($data);
00352     }
00353 
00362     public function strip_tags($sString, $sAllowableTags = '')
00363     {
00364         if (stripos($sAllowableTags, '<style>') === false) {
00365             // strip style tags with definitions within
00366             $sString = $this->preg_replace("'<style[^>]*>.*</style>'siU", '', $sString);
00367         }
00368 
00369         return strip_tags($sString, $sAllowableTags);
00370     }
00371 
00381     public function strrcmp($sStr1, $sStr2)
00382     {
00383         return -strcmp($sStr1, $sStr2);
00384     }
00385 }