Mage_Core_Helper_String Class Reference

Inheritance diagram for Mage_Core_Helper_String:

Mage_Core_Helper_Abstract

List of all members.

Public Member Functions

 truncate ($string, $length=80, $etc= '...', &$remainder= '', $breakWords=true)
 strlen ($str)
 substr ($str, $offset, $length=null)
 splitInjection ($str, $length=50, $needle= '-', $insert= ' ')
 strrev ($str)
 str_split ($str, $length=1, $keepWords=false, $trim=false, $wordSeparatorRegex= '\s')
 splitWords ($str, $uniqueOnly=false, $maxWordLenght=0, $wordSeparatorRegexp= '\s')

Public Attributes

const ICONV_CHARSET = 'UTF-8'


Detailed Description

Core data helper

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 32 of file String.php.


Member Function Documentation

splitInjection ( str,
length = 50,
needle = '-',
insert = ' ' 
)

Split string and appending $insert string after $needle

Parameters:
string $str
integer $length
string $needle
string $insert
Returns:
string

Definition at line 109 of file String.php.

00110     {
00111         $str = $this->str_split($str, $length);
00112         $newStr = '';
00113         foreach ($str as $part) {
00114             if ($this->strlen($part) >= $length) {
00115                 $lastDelimetr = iconv_strpos(strrev($part), $needle, null, self::ICONV_CHARSET);
00116                 $tmpNewStr = '';
00117                 $tmpNewStr = $this->substr(strrev($part), 0, $lastDelimetr) . $insert . $this->substr(strrev($part), $lastDelimetr);
00118                 $newStr .= strrev($tmpNewStr);
00119             } else {
00120                 $newStr .= $part;
00121             }
00122         }
00123         return $newStr;
00124     }

splitWords ( str,
uniqueOnly = false,
maxWordLenght = 0,
wordSeparatorRegexp = '\s' 
)

Split words

Parameters:
string $str The source string
bool $uniqueOnly Unique words only
int $maxWordLenght Limit words count
string $wordSeparatorRegexp
Returns:
array

Definition at line 240 of file String.php.

00241     {
00242         $result = array();
00243         $split = preg_split('#' . $wordSeparatorRegexp . '#si', $str, null, PREG_SPLIT_NO_EMPTY);
00244         foreach ($split as $key => $word) {
00245             if ($uniqueOnly) {
00246                 $result[$word] = $word;
00247             }
00248             else {
00249                 $result[] = $word;
00250             }
00251         }
00252         if ($maxWordLenght && count($result) > $maxWordLenght) {
00253             $result = array_slice($result, 0, $maxWordLenght);
00254         }
00255         return $result;
00256     }

str_split ( str,
length = 1,
keepWords = false,
trim = false,
wordSeparatorRegex = '\s' 
)

Binary-safe variant of str_split() + option not to break words + option to trim spaces (between each word) + option to set character(s) (pcre pattern) to be considered as words separator

Parameters:
string $str
int $length
bool $keepWords
bool $trim
string $wordSeparatorRegex
Returns:
array

Definition at line 158 of file String.php.

00159     {
00160         $result = array();
00161         $strlen = $this->strlen($str);
00162         if ((!$strlen) || (!is_int($length)) || ($length <= 0)) {
00163             return $result;
00164         }
00165         // trim
00166         if ($trim) {
00167             $str = trim(preg_replace('/\s{2,}/is', ' ', $str));
00168         }
00169         // do a usual str_split, but safe for our encoding
00170         if ((!$keepWords) || ($length < 2)) {
00171             for ($offset = 0; $offset < $strlen; $offset += $length) {
00172                 $result[] = iconv_substr($str, $offset, $length, self::ICONV_CHARSET);
00173             }
00174         }
00175         // split smartly, keeping words
00176         else {
00177             $split = preg_split('/(' . $wordSeparatorRegex . '+)/is', $str, null, PREG_SPLIT_DELIM_CAPTURE);
00178             $i        = 0;
00179             $space    = '';
00180             $spaceLen = 0;
00181             foreach ($split as $key => $part) {
00182                 if ($trim) {
00183                     // ignore spaces (even keys)
00184                     if ($key % 2) {
00185                         continue;
00186                     }
00187                     $space    = ' ';
00188                     $spaceLen = 1;
00189                 }
00190                 if (empty($result[$i])) {
00191                     $currentLength = 0;
00192                     $result[$i]    = '';
00193                     $space         = '';
00194                     $spaceLen      = 0;
00195                 }
00196                 else {
00197                     $currentLength = iconv_strlen($result[$i], self::ICONV_CHARSET);
00198                 }
00199                 $partLength = iconv_strlen($part, self::ICONV_CHARSET);
00200                 // add part to current last element
00201                 if (($currentLength + $spaceLen + $partLength) <= $length) {
00202                     $result[$i] .= $space . $part;
00203                 }
00204                 // add part to new element
00205                 elseif ($partLength <= $length) {
00206                     $i++;
00207                     $result[$i] = $part;
00208                 }
00209                 // break too long part recursively
00210                 else {
00211                     foreach ($this->str_split($part, $length, false, $trim, $wordSeparatorRegex) as $subpart) {
00212                         $i++;
00213                         $result[$i] = $subpart;
00214                     }
00215                 }
00216             }
00217         }
00218         // remove last element, if empty
00219         if ($count = count($result)) {
00220             if (empty($result[$count - 1])) {
00221                 unset($result[$count - 1]);
00222             }
00223         }
00224         // remove first element, if empty
00225         if (isset($result[0]) && empty($result[0])) {
00226             array_shift($result);
00227         }
00228         return $result;
00229     }

strlen ( str  ) 

Passthrough to iconv_strlen()

Parameters:
string $str
Returns:
int

Definition at line 79 of file String.php.

00080     {
00081         return iconv_strlen($str, self::ICONV_CHARSET);
00082     }

strrev ( str  ) 

Binary-safe strrev()

Parameters:
string $str
Returns:
string

Definition at line 132 of file String.php.

00133     {
00134         $result = '';
00135         $strlen = $this->strlen($str);
00136         if (!$strlen) {
00137             return $result;
00138         }
00139         for ($i = $strlen-1; $i >= 0; $i--) {
00140             $result .= iconv_substr($str, $i, 1, self::ICONV_CHARSET);
00141         }
00142         return $result;
00143     }

substr ( str,
offset,
length = null 
)

Passthrough to iconv_substr()

Parameters:
string $str
int $offset
int $length
Returns:
string

Definition at line 92 of file String.php.

00093     {
00094         if (is_null($length)) {
00095             $length = iconv_strlen($str, self::ICONV_CHARSET) - $offset;
00096         }
00097         return iconv_substr($str, $offset, $length, self::ICONV_CHARSET);
00098     }

truncate ( string,
length = 80,
etc = '...',
&$  remainder = '',
breakWords = true 
)

Truncate a string to a certain length if necessary, appending the $etc string. $remainder will contain the string that has been replaced with $etc.

Parameters:
string $string
int $length
string $etc
string &$remainder
bool $breakWords
Returns:
string

Definition at line 47 of file String.php.

00048     {
00049         $remainder = '';
00050         if (0 == $length) {
00051             return '';
00052         }
00053 
00054         $originalLength = iconv_strlen($string, self::ICONV_CHARSET);
00055         if ($originalLength > $length) {
00056             $length -= iconv_strlen($etc, self::ICONV_CHARSET);
00057             if ($length <= 0) {
00058                 return '';
00059             }
00060             $preparedString = $string;
00061             $preparedlength = $length;
00062             if (!$breakWords) {
00063                 $preparedString = preg_replace('/\s+?(\S+)?$/', '', iconv_substr($string, 0, $length + 1, self::ICONV_CHARSET));
00064                 $preparedlength = iconv_strlen($preparedString, self::ICONV_CHARSET);
00065             }
00066             $remainder = iconv_substr($string, $preparedlength, $originalLength, self::ICONV_CHARSET);
00067             return iconv_substr($preparedString, 0, $length, self::ICONV_CHARSET) . $etc;
00068         }
00069 
00070         return $string;
00071     }


Member Data Documentation

const ICONV_CHARSET = 'UTF-8'

Definition at line 34 of file String.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:55 2009 for Magento by  doxygen 1.5.8