OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxstrmb.php
Go to the documentation of this file.
1 <?php
2 
6 class oxStrMb
7 {
13  protected $_sEncoding = 'UTF-8';
14 
20  protected $_aUmls = array( "\xc3\xa4", "\xc3\xb6", "\xc3\xbc", "\xC3\x84", "\xC3\x96", "\xC3\x9C", "\xC3\x9F" );
21 
26  protected $_aUmlEntities = array('&auml;', '&ouml;', '&uuml;', '&Auml;', '&Ouml;', '&Uuml;', '&szlig;' );
27 
33  public function __construct()
34  {
35  }
36 
44  public function strlen($sStr)
45  {
46  return mb_strlen($sStr, $this->_sEncoding);
47  }
48 
58  public function substr( $sStr, $iStart, $iLength = null )
59  {
60  $iLength = is_null( $iLength ) ? $this->strlen( $sStr ) : $iLength;
61  return mb_substr( $sStr, $iStart, $iLength, $this->_sEncoding );
62  }
63 
73  public function strpos( $sHaystack, $sNeedle, $iOffset = null )
74  {
75  $iPos = false;
76  if ( $sHaystack && $sNeedle ) {
77  $iOffset = is_null( $iOffset ) ? 0 : $iOffset;
78  $iPos = mb_strpos( $sHaystack, $sNeedle, $iOffset, $this->_sEncoding );
79  }
80  return $iPos;
81  }
82 
91  public function strstr( $sHaystack, $sNeedle )
92  {
93  // additional check according to bug in PHP 5.2.0 version
94  if ( !$sHaystack ) {
95  return false;
96  }
97  return mb_strstr($sHaystack, $sNeedle, false, $this->_sEncoding);
98  }
99 
107  public function strtolower($sString)
108  {
109  return mb_strtolower($sString, $this->_sEncoding);
110  }
111 
119  public function strtoupper($sString)
120  {
121  return mb_strtoupper($sString, $this->_sEncoding);
122  }
123 
131  public function htmlspecialchars($sString)
132  {
133  return htmlspecialchars($sString, ENT_QUOTES, $this->_sEncoding);
134  }
135 
143  public function htmlentities($sString)
144  {
145  return htmlentities($sString, ENT_QUOTES, $this->_sEncoding);
146  }
147 
155  public function html_entity_decode($sString)
156  {
157  return html_entity_decode( $sString, ENT_QUOTES, $this->_sEncoding );
158  }
159 
170  public function preg_split($sPattern, $sString, $iLimit = -1, $iFlag = 0)
171  {
172  return preg_split( $sPattern.'u', $sString, $iLimit, $iFlag );
173  }
174 
186  public function preg_replace($aPattern, $sString, $sSubject, $iLimit = -1, $iCount = null)
187  {
188  if ( is_array($aPattern) ) {
189  foreach ( $aPattern as &$sPattern) {
190  $sPattern = $sPattern.'u';
191  }
192  } else {
193  $aPattern = $aPattern.'u';
194  }
195  return preg_replace( $aPattern, $sString, $sSubject, $iLimit, $iCount);
196  }
197 
209  public function preg_match($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
210  {
211  return preg_match( $sPattern.'u', $sSubject, $aMatches, $iFlags, $iOffset);
212  }
213 
225  public function preg_match_all($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
226  {
227  return preg_match_all( $sPattern.'u', $sSubject, $aMatches, $iFlags, $iOffset);
228  }
229 
237  public function ucfirst($sSubject)
238  {
239  $sString = $this->strtoupper($this->substr($sSubject, 0, 1));
240  return $sString . $this->substr($sSubject, 1);
241  }
242 
253  public function wordwrap( $sString, $iLength = 75, $sBreak = "\n", $blCut = null )
254  {
255  if ( !$blCut ) {
256  $sRegexp = "/^(.{1,{$iLength}}\r?(\s|$|\n)|.{1,{$iLength}}[^\r\s\n]*\r?(\n|\s|$))/u";
257  } else {
258  $sRegexp = "/^([^\s]{{$iLength}}|.{1,{$iLength}}\s)/u";
259  }
260 
261  $iStrLen = mb_strlen( $sString, $this->_sEncoding );
262  $iWraps = floor( $iStrLen / $iLength );
263 
264  $i = $iWraps;
265  $sReturn = '';
266  $aMatches = array();
267  while ( $i > 0 ) {
268  $iWraps = floor( mb_strlen( $sString, $this->_sEncoding ) / $iLength );
269 
270  $i = $iWraps;
271  if ( preg_match( $sRegexp, $sString, $aMatches ) ) {
272  $sStr = $aMatches[0];
273  $sReturn .= preg_replace( '/\s$/s', '', $sStr ) . $sBreak;
274  $sString = $this->substr( $sString, mb_strlen( $sStr, $this->_sEncoding ) );
275  } else {
276  break;
277  }
278  $i--;
279  }
280  $sReturn = preg_replace( "/$sBreak$/", '', $sReturn );
281  if ($sString) {
282  $sReturn .= $sBreak.$sString;
283  }
284  return $sReturn;
285  }
286 
299  public function recodeEntities( $sInput, $blToHtmlEntities = false, $aUmls = array(), $aUmlEntities = array() )
300  {
301  $aUmls = ( count( $aUmls ) > 0 ) ? array_merge( $this->_aUmls, $aUmls) : $this->_aUmls;
302  $aUmlEntities = ( count( $aUmlEntities ) > 0 ) ? array_merge( $this->_aUmlEntities, $aUmlEntities) : $this->_aUmlEntities;
303  return $blToHtmlEntities ? str_replace( $aUmls, $aUmlEntities, $sInput ) : str_replace( $aUmlEntities, $aUmls, $sInput );
304  }
305 
313  public function hasSpecialChars( $sStr )
314  {
315  return $this->preg_match( "/(".implode( "|", $this->_aUmls )."|(&amp;))/", $sStr );
316  }
317 
327  public function cleanStr( $sStr, $sCleanChr = ' ' )
328  {
329  return $this->preg_replace( "/\n|\r|\t|\xc2\x95|\xc2\xa0|;/", $sCleanChr, $sStr );
330  }
331 
339  public function jsonEncode($data)
340  {
341  return json_encode($data);
342  }
343 
352  public function strip_tags( $sString, $sAllowableTags = '' )
353  {
354  if ( stripos( $sAllowableTags, '<style>' ) === false ) {
355  // strip style tags with definitions within
356  $sString = $this->preg_replace( "'<style[^>]*>.*</style>'siU", '', $sString );
357  }
358  return strip_tags( $sString, $sAllowableTags );
359  }
360 
370  public function strrcmp( $sStr1, $sStr2 )
371  {
372  return -strcmp( $sStr1, $sStr2 );
373  }
374 }