Retrieves the byte position of a substring within a string.
bindex(string, substring [, start_position]
)
$i = bindex("hello there", "the");
Table 3.87. Arguments and Return Values for bindex()
Argument Type | Return Type | Description |
---|---|---|
String, String, [Integer] | Integer | If the substring is found, returns the byte position of the substring within the string (starting at 0). If not found, returns -1. If an offset position is given, the search starts at the offset position. All values are byte positions, not character positions, which may differ for multi-byte character encodings. |
This function does not throw any exceptions.
Returns the starting byte position of a string in another, starting from the end of the string (-1 if not found) and takes an optional starting position.
brindex(string, substring, [position]
)
$i = brindex("hello there", "the");
Table 3.88. Arguments and Return Values for brindex()
Argument Type | Return Type | Description |
---|---|---|
String, String, [Integer] | Integer | Returns the starting byte position of a string in another, starting from the end of the string, or from |
This function does not throw any exceptions.
Removes the trailing end-of-line indicator ('\n' or '\r\n') from a string and returns the new string (also see the chomp operator). If no EOL indicator is present in the string, this function simply returns the original string unmodified. This function accepts variable references, in which case it will modify the string in place and also return the modified string.
chomp(string
)
$line = chomp("hello\n"); # returns "hello"
Table 3.89. Arguments and Return Values for chomp()
Argument Type | Return Type | Description |
---|---|---|
String | String | Returns the new string with any end-of-line character(s) removed; if the first argument is a variable reference, then the string is modified in place and the new string is also returned. |
This function does not throw any exceptions.
Returns a string containing a single ASCII character represented by the numeric value passed.
chr(integer
)
$i = chr(65); # returns "A"
Table 3.90. Arguments and Return Values for chr()
Argument Type | Return Type | Description |
---|---|---|
Integer | String | Returns a string containing a single ASCII character represented by the numeric value passed. |
This function does not throw any exceptions.
Performs explicit string character encoding conversions.
convert_encoding(string, new_encoding
)
$utf8_str = convert_encoding($iso_8859_1_str, "utf-8");
Table 3.91. Arguments and Return Values for convert_encoding()
Argument Type | Return Type | Description |
---|---|---|
String, String | String | Converts the string arguement to the encoding given and returns the new string. |
Table 3.92. Exceptions Thrown by convert_encoding()
err | desc |
---|---|
| There was an error converting to the target encoding (ex: conversion not supported, illegal character sequence, etc). |
Returns the first string argument tagged with the character encoding given as the second argument; does not actually change the string data; use only in the case that a string is tagged with the wrong encoding, for example, if a string from a File object has a different encoding than the File object.
force_encoding(string, new_encoding
)
$utf8_str = force_encoding($bad_str, "utf-8");
Table 3.93. Arguments and Return Values for convert_encoding()
Argument Type | Return Type | Description |
---|---|---|
String, String | String | Returns a string with identical byte data as the input string, but tagged with the new encoding. |
This function does not throw any exceptions.
Returns a string describing the character encoding of the string passed.
get_encoding(string
)
$enc = get_encoding($string);
Table 3.94. Arguments and Return Values for get_encoding()
Argument Type | Return Type | Description |
---|---|---|
String | String | Returns a string describing the character encoding of the string passed (ex: "UTF-8", "ISO-8850-1", "KOI8-R"). |
This function does not throw any exceptions.
Retrieves the character position of a substring within a string.
index(string, substring [, start_position]
)
$i = index("hello there", "the");
Table 3.95. Arguments and Return Values for index()
Argument Type | Return Type | Description |
---|---|---|
String, String, [Integer] | Integer | If the substring is found, returns the position of the substring within the string (starting at 0). If not found, returns -1. If an offset position is given, the search starts at the offset position. All values are character positions, not byte positions, which may differ for multi-byte character encodings. |
This function does not throw any exceptions.
Creates a string from a list and separator string.
join(separator_string, list
)
$str = join(":", ("a", "b", "c")); # returns "a:b:c"
Table 3.96. Arguments and Return Values for join()
Argument Type | Return Type | Description |
---|---|---|
String, List | Srring | Returns a string with each element of the list separated by the separator string. |
This function does not throw any exceptions.
Returns the length in characters for the string passed. Note that the byte length may differ from the character length with multi-byte character encodings. For byte length of a string, see strlen().
length(string
)
$len = length("hello"); # returns 5
Table 3.97. Arguments and Return Values for length()
Argument Type | Return Type | Description |
---|---|---|
String | Integer | Returns the length in characters for the string passed. |
This function does not throw any exceptions.
Gives the numeric value of the first character in the string passed.
ord(string
)
$i = ord("A"); # returns 65
Table 3.98. Arguments and Return Values for ord()
Argument Type | Return Type | Description |
---|---|---|
String | Integer | Gives the numeric value of the first character in the string passed. Only works reliably with character encodings where each character is a single byte. |
This function does not throw any exceptions.
Returns True if the regular expression matches the string passed, otherwise returns False.
regex(string, pattern_string, [options]
)
$bool = regex("hello", "^hel"); # returns True
Table 3.99. Arguments and Return Values for regex()
Argument Type | Return Type | Description |
---|---|---|
String, String | Boolean | Returns True if |
Table 3.100. Exceptions Thrown by regex()
err | desc |
---|---|
| There was an error compiling the regular expression. |
For more information on regular expression processing, see Regular Expressions.
Returns a list of substrings in a string based on matching patterns defined by a regular expression.
regex_extract(string, pattern_string, [options]
)
$list = regex_extract("hello:there", "(\\w+):(\\w+)");
Table 3.101. Arguments and Return Values for regex_extract()
Argument Type | Return Type | Description |
---|---|---|
String, String, [Integer] | String | Returns the result of |
Table 3.102. Exceptions Thrown by regex_extract()
err | desc |
---|---|
| Invalid options were passed to the function. |
| There was an error compiling the regular expression. |
For more information on regular expression processing, see Regular Expressions.
Returns a string with patterns substituted according to the arguments passed.
regex_subst(string, pattern_string, target_string, [options]
)
$str = regex_subst("hello there", "^there$", "you"); # returns "hello you"
Table 3.103. Arguments and Return Values for regex_subst()
Argument Type | Return Type | Description |
---|---|---|
String, String, String, [Integer] | String | Returns the result of |
Table 3.104. Exceptions Thrown by regex_subst()
err | desc |
---|---|
| Invalid options were passed to the function. |
| There was an error compiling the regular expression. |
For more information on regular expression processing, see Regular Expressions.
Replaces all occurrances of a substring in a string with another string.
replace(string, substring, new_substring
)
$str = replace("hello there", "there", "you"); # returns "hello you"
Table 3.105. Arguments and Return Values for replace()
Argument Type | Return Type | Description |
---|---|---|
String, String, String | String | Replaces all occurrances of a substring in a string with another string and returns the new string. |
This function does not throw any exceptions.
Returns the starting character position of a string in another, starting from the end of the string (-1 if not found) and takes an optional starting position.
rindex(string, substring, [position]
)
$i = rindex("hello there", "the");
Table 3.106. Arguments and Return Values for rindex()
Argument Type | Return Type | Description |
---|---|---|
String, String, [Integer] | Integer | Returns the starting character position of a string in another, starting from the end of the string, or from |
This function does not throw any exceptions.
Splits a string into a list of components based on a separator string.
split(pattern, string
)
$list = split(":", "some:text:here"); # returns ("some", "text", "here")
Table 3.107. Arguments and Return Values for split()
Argument Type | Return Type | Description |
---|---|---|
String, String | List | Returns a list of each component of a string separated by a separator string, with the separator removed. |
This function does not throw any exceptions.
Returns the length in bytes of the string argument. Note that the byte length may differ from the character length with multi-byte character encodings. For the character length of a string, see length().
strlen(string
)
$len = strlen("hello"); # returns 5
Table 3.108. Arguments and Return Values for strlen()
Argument Type | Return Type | Description |
---|---|---|
String | Integer | Returns the length of the string passed. If the argument is not a string, then it is converted to a string. |
This function does not throw any exceptions.
Returns a portion of a string starting from an integer offset, with an optional length. Arguments can be negative, giving offsets from the end of the string. All offsets are character positions, not byte positions.
substr(string, offset, [length]
)
$str = substr("hello there", 6); # returns "there"
Table 3.109. Arguments and Return Values for substr()
Argument Type | Return Type | Description |
---|---|---|
String, Integer, [Integer] | String | Returns the substring according to the arguments. If |
This function does not throw any exceptions.
Converts the argument passed to a string value all in lower case.
tolower(string
)
$str = tolower("HELLO"); # returns "hello"
Table 3.110. Arguments and Return Values for tolower()
Argument Type | Return Type | Description |
---|---|---|
String | String | Converts argument passed to a string value, all in lower case. |
This function does not throw any exceptions.
Converts the argument passed to a string value all in upper case.
toupper(string
)
$str = toupper("hello"); # returns "HELLO"
Table 3.111. Arguments and Return Values for toupper()
Argument Type | Return Type | Description |
---|---|---|
Any | String | Converts argument passed to a string value, all in upper case. |
This function does not throw any exceptions.
Removes characters from the start and end of a string and returns the new string (also see the trim operator). This function accepts variable references, in which case it will modify the string in place and also return the modified string.
By default the following whitespace characters are removed: ' ', '\n', '\r', '\t', '\v' (vertical tab, ASCII 11), and '\0' (null character). To trim other character, pass a string as the second argument specifying the characters to be removed.
trim(string, [chars_to_trim]
)
$line = trim(" hello \n"); # returns "hello"
Table 3.112. Arguments and Return Values for trim()
Argument Type | Return Type | Description |
---|---|---|
String, [String] | String | Returns the new string with characters removed from the beginning and end of the string; if the first argument is a variable reference, then the string is modified in place and the new string is also returned. |
This function does not throw any exceptions.