JOOMLA中国
  • Joomla中国首页
  • 社区
  • 教程
  • 应用市场
  • B计划
Joomla! Framework TM
  • Namespace
  • Class
  • Tree
  • Deprecated

Namespaces

  • Composer
    • Autoload
  • Joomla
    • Application
      • Cli
        • Output
          • Processor
      • Web
    • Data
    • DI
      • Exception
    • Event
    • Filter
    • Input
    • Ldap
    • Registry
      • Format
    • Session
      • Storage
    • String
    • Uri
    • Utilities
  • None
  • PasswordCompat
    • binary
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Yaml
        • Exception
    • Polyfill
      • Util

Classes

  • Inflector
  • Normalise
  • String
  • StringHelper

Class StringHelper

String handling class for UTF-8 data wrapping the phputf8 library. All functions assume the validity of UTF-8 strings.

Direct known subclasses

Joomla\String\String, JString

Abstract
Namespace: Joomla\String
Since: 1.3.0
Located at vendor/joomla/string/src/StringHelper.php

Methods summary

public static string
# increment( string $string, string $style = 'default', integer $n = 0 )

Increments a trailing number in a string.

Increments a trailing number in a string.

Used to easily create distinct labels when copying objects. The method has the following styles:

default: "Label" becomes "Label (2)" dash: "Label" becomes "Label-2"

Parameters

$string
string - $string The source string.
$style
string - $style The the style (default|dash).
$n
integer - $n If supplied, this number is used for the copy, otherwise it is the 'next' number.

Returns

string - The incremented string.

Since

1.3.0
public static boolean
# is_ascii( string $str )

Tests whether a string contains only 7bit ASCII bytes.

Tests whether a string contains only 7bit ASCII bytes.

You might use this to conditionally check whether a string needs handling as UTF-8 or not, potentially offering performance benefits by using the native PHP equivalent if it's just ASCII e.g.;

if (StringHelper::is_ascii($someString))
{
    // It's just ASCII - use the native PHP version
    $someString = strtolower($someString);
}
else
{
    $someString = StringHelper::strtolower($someString);
}

Parameters

$str
string - $str The string to test.

Returns

boolean - True if the string is all ASCII

Since

1.3.0
public static integer
# ord( string $chr )

UTF-8 aware alternative to ord()

UTF-8 aware alternative to ord()

Returns the unicode ordinal for a character.

Parameters

$chr
string - $chr UTF-8 encoded character

Returns

integer - Unicode ordinal for the character

Since

1.4.0

See

http://www.php.net/ord
public static mixed
# strpos( string $str, string $search, integer $offset = false )

UTF-8 aware alternative to strpos()

UTF-8 aware alternative to strpos()

Find position of first occurrence of a string.

Parameters

$str
string - $str String being examined
$search
string - $search String being searched for
$offset
integer - $offset Optional, specifies the position from which the search should be performed

Returns

mixed - Number of characters before the first match or FALSE on failure

Since

1.3.0

See

http://www.php.net/strpos
public static mixed
# strrpos( string $str, string $search, integer $offset = 0 )

UTF-8 aware alternative to strrpos()

UTF-8 aware alternative to strrpos()

Finds position of last occurrence of a string.

Parameters

$str
string - $str String being examined.
$search
string - $search String being searched for.
$offset
integer - $offset Offset from the left of the string.

Returns

mixed - Number of characters before the last match or false on failure

Since

1.3.0

See

http://www.php.net/strrpos
public static mixed
# substr( string $str, integer $offset, integer $length = false )

UTF-8 aware alternative to substr()

UTF-8 aware alternative to substr()

Return part of a string given character offset (and optionally length).

Parameters

$str
string - $str String being processed
$offset
integer - $offset Number of UTF-8 characters offset (from left)
$length
integer - $length Optional length in UTF-8 characters from offset

Returns

mixed - string or FALSE if failure

Since

1.3.0

See

http://www.php.net/substr
public static mixed
# strtolower( string $str )

UTF-8 aware alternative to strtolower()

UTF-8 aware alternative to strtolower()

Make a string lowercase

Note: The concept of a characters "case" only exists is some alphabets such as Latin, Greek, Cyrillic, Armenian and archaic Georgian - it does not exist in the Chinese alphabet, for example. See Unicode Standard Annex #21: Case Mappings

Parameters

$str
string - $str String being processed

Returns

mixed - Either string in lowercase or FALSE is UTF-8 invalid

Since

1.3.0

See

http://www.php.net/strtolower
public static mixed
# strtoupper( string $str )

UTF-8 aware alternative to strtoupper()

UTF-8 aware alternative to strtoupper()

Make a string uppercase

Note: The concept of a characters "case" only exists is some alphabets such as Latin, Greek, Cyrillic, Armenian and archaic Georgian - it does not exist in the Chinese alphabet, for example. See Unicode Standard Annex #21: Case Mappings

Parameters

$str
string - $str String being processed

Returns

mixed - Either string in uppercase or FALSE is UTF-8 invalid

Since

1.3.0

See

http://www.php.net/strtoupper
public static integer
# strlen( string $str )

UTF-8 aware alternative to strlen()

UTF-8 aware alternative to strlen()

Returns the number of characters in the string (NOT THE NUMBER OF BYTES).

Parameters

$str
string - $str UTF-8 string.

Returns

integer - Number of UTF-8 characters in string.

Since

1.3.0

See

http://www.php.net/strlen
public static string
# str_ireplace( string $search, string $replace, string $str, integer $count = null )

UTF-8 aware alternative to str_ireplace()

UTF-8 aware alternative to str_ireplace()

Case-insensitive version of str_replace()

Parameters

$search
string - $search String to search
$replace
string - $replace Existing string to replace
$str
string - $str New string to replace with
$count
integer - $count Optional count value to be passed by referene

Returns

string - UTF-8 String

Since

1.3.0

See

http://www.php.net/str_ireplace
public static string
# str_pad( string $input, integer $length, string $padStr = ' ', integer $type = STR_PAD_RIGHT )

UTF-8 aware alternative to str_pad()

UTF-8 aware alternative to str_pad()

Pad a string to a certain length with another string. $padStr may contain multi-byte characters.

Parameters

$input
string - $input The input string.
$length
integer - $length If the value is negative, less than, or equal to the length of the input string, no padding takes place.
$padStr
string - $padStr The string may be truncated if the number of padding characters can't be evenly divided by the string's length.
$type
integer - $type The type of padding to apply

Returns

string

Since

1.4.0

See

http://www.php.net/str_pad
public static array
# str_split( string $str, integer $split_len = 1 )

UTF-8 aware alternative to str_split()

UTF-8 aware alternative to str_split()

Convert a string to an array.

Parameters

$str
string - $str UTF-8 encoded string to process
$split_len
integer - $split_len Number to characters to split string by

Returns

array

Since

1.3.0

See

http://www.php.net/str_split
public static integer
# strcasecmp( string $str1, string $str2, mixed $locale = false )

UTF-8/LOCALE aware alternative to strcasecmp()

UTF-8/LOCALE aware alternative to strcasecmp()

A case insensitive string comparison.

Parameters

$str1
string - $str1 string 1 to compare
$str2
string - $str2 string 2 to compare
$locale
mixed - $locale The locale used by strcoll or false to use classical comparison

Returns

integer - < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Since

1.3.0

See

http://www.php.net/strcasecmp
http://www.php.net/strcoll
http://www.php.net/setlocale
public static integer
# strcmp( string $str1, string $str2, mixed $locale = false )

UTF-8/LOCALE aware alternative to strcmp()

UTF-8/LOCALE aware alternative to strcmp()

A case sensitive string comparison.

Parameters

$str1
string - $str1 string 1 to compare
$str2
string - $str2 string 2 to compare
$locale
mixed - $locale The locale used by strcoll or false to use classical comparison

Returns

integer - < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Since

1.3.0

See

http://www.php.net/strcmp
http://www.php.net/strcoll
http://www.php.net/setlocale
public static integer
# strcspn( string $str, string $mask, integer $start = null, integer $length = null )

UTF-8 aware alternative to strcspn()

UTF-8 aware alternative to strcspn()

Find length of initial segment not matching mask.

Parameters

$str
string - $str The string to process
$mask
string - $mask The mask
$start
integer - $start Optional starting character position (in characters)
$length
integer - $length Optional length

Returns

integer - The length of the initial segment of str1 which does not contain any of the characters in str2

Since

1.3.0

See

http://www.php.net/strcspn
public static string
# stristr( string $str, string $search )

UTF-8 aware alternative to stristr()

UTF-8 aware alternative to stristr()

Returns all of haystack from the first occurrence of needle to the end. Needle and haystack are examined in a case-insensitive manner to find the first occurrence of a string using case insensitive comparison.

Parameters

$str
string - $str The haystack
$search
string - $search The needle

Returns

string - the sub string

Since

1.3.0

See

http://www.php.net/stristr
public static string
# strrev( string $str )

UTF-8 aware alternative to strrev()

UTF-8 aware alternative to strrev()

Reverse a string.

Parameters

$str
string - $str String to be reversed

Returns

string - The string in reverse character order

Since

1.3.0

See

http://www.php.net/strrev
public static integer
# strspn( string $str, string $mask, integer $start = null, integer $length = null )

UTF-8 aware alternative to strspn()

UTF-8 aware alternative to strspn()

Find length of initial segment matching mask.

Parameters

$str
string - $str The haystack
$mask
string - $mask The mask
$start
integer - $start Start optional
$length
integer - $length Length optional

Returns

integer

Since

1.3.0

See

http://www.php.net/strspn
public static string
# substr_replace( string $str, string $repl, integer $start, integer $length = null )

UTF-8 aware alternative to substr_replace()

UTF-8 aware alternative to substr_replace()

Replace text within a portion of a string.

Parameters

$str
string - $str The haystack
$repl
string - $repl The replacement string
$start
integer - $start Start
$length
integer - $length Length (optional)

Returns

string

Since

1.3.0

See

http://www.php.net/substr_replace
public static string
# ltrim( string $str, string $charlist = false )

UTF-8 aware replacement for ltrim()

UTF-8 aware replacement for ltrim()

Strip whitespace (or other characters) from the beginning of a string. You only need to use this if you are supplying the charlist optional arg and it contains UTF-8 characters. Otherwise ltrim will work normally on a UTF-8 string.

Parameters

$str
string - $str The string to be trimmed
$charlist
string - $charlist The optional charlist of additional characters to trim

Returns

string - The trimmed string

Since

1.3.0

See

http://www.php.net/ltrim
public static string
# rtrim( string $str, string $charlist = false )

UTF-8 aware replacement for rtrim()

UTF-8 aware replacement for rtrim()

Strip whitespace (or other characters) from the end of a string. You only need to use this if you are supplying the charlist optional arg and it contains UTF-8 characters. Otherwise rtrim will work normally on a UTF-8 string.

Parameters

$str
string - $str The string to be trimmed
$charlist
string - $charlist The optional charlist of additional characters to trim

Returns

string - The trimmed string

Since

1.3.0

See

http://www.php.net/rtrim
public static string
# trim( string $str, string $charlist = false )

UTF-8 aware replacement for trim()

UTF-8 aware replacement for trim()

Strip whitespace (or other characters) from the beginning and end of a string. You only need to use this if you are supplying the charlist optional arg and it contains UTF-8 characters. Otherwise trim will work normally on a UTF-8 string

Parameters

$str
string - $str The string to be trimmed
$charlist
string - $charlist The optional charlist of additional characters to trim

Returns

string - The trimmed string

Since

1.3.0

See

http://www.php.net/trim
public static string
# ucfirst( string $str, string $delimiter = null, string $newDelimiter = null )

UTF-8 aware alternative to ucfirst()

UTF-8 aware alternative to ucfirst()

Make a string's first character uppercase or all words' first character uppercase.

Parameters

$str
string - $str String to be processed
$delimiter
string - $delimiter The words delimiter (null means do not split the string)
$newDelimiter
string - $newDelimiter The new words delimiter (null means equal to $delimiter)

Returns

string - If $delimiter is null, return the string with first character as upper case (if applicable) else consider the string of words separated by the delimiter, apply the ucfirst to each words and return the string with the new delimiter

Since

1.3.0

See

http://www.php.net/ucfirst
public static string
# ucwords( string $str )

UTF-8 aware alternative to ucwords()

UTF-8 aware alternative to ucwords()

Uppercase the first character of each word in a string.

Parameters

$str
string - $str String to be processed

Returns

string - String with first char of each word uppercase

Since

1.3.0

See

http://www.php.net/ucwords
public static mixed
# transcode( string $source, string $from_encoding, string $to_encoding )

Transcode a string.

Transcode a string.

Parameters

$source
string - $source The string to transcode.
$from_encoding
string - $from_encoding The source encoding.
$to_encoding
string - $to_encoding The target encoding.

Returns

mixed - The transcoded string, or null if the source was not a string.

Since

1.3.0

Link

https://bugs.php.net/bug.php?id=48147
public static boolean
# valid( string $str )

Tests a string as to whether it's valid UTF-8 and supported by the Unicode standard.

Tests a string as to whether it's valid UTF-8 and supported by the Unicode standard.

Note: this function has been modified to simple return true or false.

Parameters

$str
string - $str UTF-8 encoded string.

Returns

boolean - true if valid

Author

<hsivonen@iki.fi>

Since

1.3.0

See

http://hsivonen.iki.fi/php-utf8/
Joomla\String\StringHelper::compliant()
public static boolean
# compliant( string $str )

Tests whether a string complies as UTF-8.

Tests whether a string complies as UTF-8.

This will be much faster than StringHelper::valid() but will pass five and six octet UTF-8 sequences, which are not supported by Unicode and so cannot be displayed correctly in a browser. In other words it is not as strict as StringHelper::valid() but it's faster. If you use it to validate user input, you place yourself at the risk that attackers will be able to inject 5 and 6 byte sequences (which may or may not be a significant risk, depending on what you are are doing).

Parameters

$str
string - $str UTF-8 string to check

Returns

boolean - TRUE if string is valid UTF-8

Since

1.3.0

See

Joomla\String\StringHelper::valid()
http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php#54805
public static string
# unicode_to_utf8( string $str )

Converts Unicode sequences to UTF-8 string.

Converts Unicode sequences to UTF-8 string.

Parameters

$str
string - $str Unicode string to convert

Returns

string - UTF-8 string

Since

1.3.0
public static string
# unicode_to_utf16( string $str )

Converts Unicode sequences to UTF-16 string.

Converts Unicode sequences to UTF-16 string.

Parameters

$str
string - $str Unicode string to convert

Returns

string - UTF-16 string

Since

1.3.0

Magic methods summary

Properties summary

protected static array $incrementStyles
#

Increment styles.

Increment styles.

Since

1.3.0
Joomla! Framework TM API documentation generated by ApiGen 2.8.0
Joomla!® and Joomla! Framework™ are trademarks of Open Source Matters, Inc. in the United States and other countries.