OXID eShop CE  4.10.5
 All Classes Namespaces Files Functions Variables Pages
oxstrmb.php
Go to the documentation of this file.
1 <?php
2 
6 class oxStrMb
7 {
8 
14  protected $_sEncoding = 'UTF-8';
15 
21  protected $_aUmls = array("\xc3\xa4", "\xc3\xb6", "\xc3\xbc", "\xC3\x84", "\xC3\x96", "\xC3\x9C", "\xC3\x9F");
22 
28  protected $_aUmlEntities = array('&auml;', '&ouml;', '&uuml;', '&Auml;', '&Ouml;', '&Uuml;', '&szlig;');
29 
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 
62  return mb_substr($sStr, $iStart, $iLength, $this->_sEncoding);
63  }
64 
74  public function strpos($sHaystack, $sNeedle, $iOffset = null)
75  {
76  $iPos = false;
77  if ($sHaystack && $sNeedle) {
78  $iOffset = is_null($iOffset) ? 0 : $iOffset;
79  $iPos = mb_strpos($sHaystack, $sNeedle, $iOffset, $this->_sEncoding);
80  }
81 
82  return $iPos;
83  }
84 
93  public function strstr($sHaystack, $sNeedle)
94  {
95  // additional check according to bug in PHP 5.2.0 version
96  if (!$sHaystack) {
97  return false;
98  }
99 
100  return mb_strstr($sHaystack, $sNeedle, false, $this->_sEncoding);
101  }
102 
110  public function strtolower($sString)
111  {
112  return mb_strtolower($sString, $this->_sEncoding);
113  }
114 
122  public function strtoupper($sString)
123  {
124  return mb_strtoupper($sString, $this->_sEncoding);
125  }
126 
135  public function htmlspecialchars($sString, $iQuotStyle = ENT_QUOTES)
136  {
137  return htmlspecialchars($sString, $iQuotStyle, $this->_sEncoding);
138  }
139 
148  public function htmlentities($sString, $iQuotStyle = ENT_QUOTES)
149  {
150  return htmlentities($sString, $iQuotStyle, $this->_sEncoding);
151  }
152 
161  public function html_entity_decode($sString, $iQuotStyle = ENT_QUOTES)
162  {
163  return html_entity_decode($sString, $iQuotStyle, $this->_sEncoding);
164  }
165 
176  public function preg_split($sPattern, $sString, $iLimit = -1, $iFlag = 0)
177  {
178  return preg_split($sPattern . 'u', $sString, $iLimit, $iFlag);
179  }
180 
192  public function preg_replace($aPattern, $sString, $sSubject, $iLimit = -1, $iCount = null)
193  {
194  if (is_array($aPattern)) {
195  foreach ($aPattern as &$sPattern) {
196  $sPattern = $sPattern . 'u';
197  }
198  } else {
199  $aPattern = $aPattern . 'u';
200  }
201 
202  return preg_replace($aPattern, $sString, $sSubject, $iLimit, $iCount);
203  }
204 
216  public function preg_replace_callback($pattern, $callback, $subject, $limit = -1, &$count = null)
217  {
218  if (is_array($pattern)) {
219  foreach ($pattern as &$item) {
220  $item = $item . 'u';
221  }
222  } else {
223  $pattern = $pattern . 'u';
224  }
225 
226  return preg_replace_callback($pattern, $callback, $subject, $limit, $count);
227  }
228 
240  public function preg_match($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
241  {
242  return preg_match($sPattern . 'u', $sSubject, $aMatches, $iFlags, $iOffset);
243  }
244 
256  public function preg_match_all($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
257  {
258  return preg_match_all($sPattern . 'u', $sSubject, $aMatches, $iFlags, $iOffset);
259  }
260 
268  public function ucfirst($sSubject)
269  {
270  $sString = $this->strtoupper($this->substr($sSubject, 0, 1));
271 
272  return $sString . $this->substr($sSubject, 1);
273  }
274 
285  public function wordwrap($sString, $iLength = 75, $sBreak = "\n", $blCut = null)
286  {
287  if (!$blCut) {
288  $sRegexp = "/^(.{1,{$iLength}}\r?(\s|$|\n)|.{1,{$iLength}}[^\r\s\n]*\r?(\n|\s|$))/u";
289  } else {
290  $sRegexp = "/^([^\s]{{$iLength}}|.{1,{$iLength}}\s)/u";
291  }
292 
293  $iStrLen = mb_strlen($sString, $this->_sEncoding);
294  $iWraps = floor($iStrLen / $iLength);
295 
296  $i = $iWraps;
297  $sReturn = '';
298  $aMatches = array();
299  while ($i > 0) {
300  $iWraps = floor(mb_strlen($sString, $this->_sEncoding) / $iLength);
301 
302  $i = $iWraps;
303  if (preg_match($sRegexp, $sString, $aMatches)) {
304  $sStr = $aMatches[0];
305  $sReturn .= preg_replace('/\s$/s', '', $sStr) . $sBreak;
306  $sString = $this->substr($sString, mb_strlen($sStr, $this->_sEncoding));
307  } else {
308  break;
309  }
310  $i--;
311  }
312  $sReturn = preg_replace("/$sBreak$/", '', $sReturn);
313  if ($sString) {
314  $sReturn .= $sBreak . $sString;
315  }
316 
317  return $sReturn;
318  }
319 
332  public function recodeEntities($sInput, $blToHtmlEntities = false, $aUmls = array(), $aUmlEntities = array())
333  {
334  $aUmls = (count($aUmls) > 0) ? array_merge($this->_aUmls, $aUmls) : $this->_aUmls;
335  $aUmlEntities = (count($aUmlEntities) > 0) ? array_merge($this->_aUmlEntities, $aUmlEntities) : $this->_aUmlEntities;
336 
337  return $blToHtmlEntities ? str_replace($aUmls, $aUmlEntities, $sInput) : str_replace($aUmlEntities, $aUmls, $sInput);
338  }
339 
347  public function hasSpecialChars($sStr)
348  {
349  return $this->preg_match("/(" . implode("|", $this->_aUmls) . "|(&amp;))/", $sStr);
350  }
351 
361  public function cleanStr($sStr, $sCleanChr = ' ')
362  {
363  return $this->preg_replace("/\n|\r|\t|\xc2\x95|\xc2\xa0|;/", $sCleanChr, $sStr);
364  }
365 
373  public function jsonEncode($data)
374  {
375  return json_encode($data);
376  }
377 
386  public function strip_tags($sString, $sAllowableTags = '')
387  {
388  if (stripos($sAllowableTags, '<style>') === false) {
389  // strip style tags with definitions within
390  $sString = $this->preg_replace("'<style[^>]*>.*</style>'siU", '', $sString);
391  }
392 
393  return strip_tags($sString, $sAllowableTags);
394  }
395 
405  public function strrcmp($sStr1, $sStr2)
406  {
407  return -strcmp($sStr1, $sStr2);
408  }
409 }