<?php$txt= "Hello world!"; Echo $txt;? >
With a simple understanding of the PHP data type in the previous article, we know the string type, this article will introduce the string functions commonly used in PHP.
The string is a sequence of characters, such as "Hello world!".
PHP String Functions
What is the role of string in PHP?
String variables are used to store and manipulate text.
String variables are used to contain values that have characters.
After the string has been created, we can manipulate it. You can use a string directly in a function, or store it in a variable.
In the following example, we create a string variable named TXT and assign a value of "Hello world!". Then we output the value of the TXT variable:
<?php$txt= "Hello world!"; Echo $txt;? >
Operation Result:
Hello world!
Note: When you assign a text value to a variable, remember to add single or double quotation marks to the text value.
Some common functions and operators for manipulating strings.
PHP collocated operator
In PHP, there is only one string operator.
The collocated operator (.) is used to concatenate two string values together.
The following example shows how to concatenate two string variables together:
<?php$txt1= "Hello world!"; $txt 2= "What a nice day!"; echo $txt 1. " " . $txt 2;? >
Operation Result:
Hello world! What a nice day!
hint: in the above code, we have used two times the collocated operator. This is because we need to insert a space between the two strings.
1. strlen () function
The strlen () function returns the length of a string, measured in characters.
The following example returns the length of the string "Hello world!":
<?phpecho strlen ("Hello world!");? >
Operation Result:
12
Tip: strlen () is commonly used in loops and other functions when it is important to determine when a string ends. (for example, in a loop, we might need to stop looping after the last character of the string).
2. Strpos () function
The Strpos () function is used to retrieve the specified character or text within a string.
If a match is found, the first matching character position is returned. If no match is found, FALSE is returned.
The following example retrieves the text "World" in the string "Hello world!":
<?phpecho Strpos ("Hello world!", "World");? >
Operation Result:
6
Tip: The position of the string "World" in the example above is 6. The reason for 6 (instead of 7) is that the position of the first character in the string is 0 instead of 1.
3. Addcslashes ()//infrequently used
The Addcslashes () function returns a string that adds a backslash before the specified character.
Note: the Addcslashes () function is case-sensitive.
Note: Be careful when applying addcslashes () to the following characters: 0 (NULL), R (carriage return), N (newline), F for page break), T (tab), and V (Vertical tab). In PHP, the \ r, \ n, \ t, \f, and \v are predefined escape sequences.
<?php$str = Addcslashes ("A001 A002 A003", "A"); Echo ($STR);? >
Operation Result:
\a001 \a002 \a003
To add a backslash to a character in a range within a string:
<?php$str = "Welcome to shanghai!"; echo $str. " <br> "; Echo addcslashes ($str, ' A. Z '). " <br> "; Echo addcslashes ($str, ' A. Z '). " <br> "; Echo addcslashes ($str, ' A. G ');? >
Operation Result:
Welcome to Shanghai!\welcome to \shanghai! W\e\l\c\o\m\e \t\o s\h\a\n\g\h\a\i! W\el\com\e to sh\an\gh\ai!
4. Addslashes ()//infrequently used
The Addslashes () function returns a string that adds a backslash before a predefined character.
The predefined characters are:
- Single quotation mark (')
- Double quotation marks (")
- Back slash (\)
- Null
Tip: This function can be used to prepare strings for strings stored in the database as well as database query statements.
Note: By default, PHP automatically runs Addslashes () for all GET, POST, and COOKIE data. So you should not use Addslashes () for a string that has been escaped, because this results in double-layer escaping. You can use the function GET_MAGIC_QUOTES_GPC () to detect this situation.
PHP String functions are part of the PHP core. These functions can be used without installation.
To add a backslash to a predefined character in a string:
<?php$str = "Who ' s Bill Gates?"; Echo $str. "This was not safe in a database query.<br>"; Echo addslashes ($STR). "This is safe in a database query."? >
Operation Result:
Who ' s Bill Gates? This is the not safe in a database query. Who\ ' s Bill Gates? This is safe in a database query.
5. Bin2Hex ()//infrequently used
The Bin2Hex () function converts a string of ASCII characters to a hexadecimal value. Strings can be converted back by using the pack () function.
Convert a string value from binary to 16 and back again:
<?PHP$STR = "Shanghai"; Echo Bin2Hex ($STR). "<br>"; Echo Pack ("h*", Bin2Hex ($STR)). "<br>";? >
Operation Result:
5368616e67686169shanghai
Tip: The Bin2Hex () converted string is 16 bits.
Tip: These functions are all from php5, and other or later versions need to be checked for availability when applying functions.
function |
Description |
Addcslashes () |
Returns a string that adds a backslash before the specified character. |
Addslashes () |
Returns a string that adds a backslash before a predefined character. |
Bin2Hex () |
Converts a string of ASCII characters to a hexadecimal value. |
Chop () |
Removes white space characters or other characters to the right of a string. |
Chr () |
Returns the character from the specified ASCII value. |
Chunk_split () |
Splits a string into a series of smaller parts. |
Convert_cyr_string () |
Converts a string from one Cyrillic character set to another. |
Convert_uudecode () |
Decodes a uuencode encoded string. |
Convert_uuencode () |
Encodes a string using the uuencode algorithm. |
Count_chars () |
Returns information about the characters used in the string. |
CRC32 () |
Computes a 32-bit CRC of a string. |
Crypt () |
One-way string encryption (hashing). |
Echo () |
Outputs one or more strings. |
Explode () |
Breaks the string into arrays. |
fprintf () |
Writes a formatted string to the specified output stream. |
Get_html_translation_table () |
Returns the translation tables used by Htmlspecialchars () and Htmlentities (). |
Hebrev () |
Converts the Hebrew text to visible text. |
HEBREVC () |
Converts the Hebrew text to visible text and converts the new line (\ n) to <br>. |
Hex2bin () |
Converts a string of hexadecimal values to ASCII characters. |
Html_entity_decode () |
Converts an HTML entity to a character. |
Htmlentities () |
Converts a character to an HTML entity. |
Htmlspecialchars_decode () |
Converts some of the predefined HTML entities to characters. |
Htmlspecialchars () |
Convert some of the predefined characters to HTML entities. |
Implode () |
Returns a string that is composed of array elements. |
Join () |
The alias of the Implode (). |
Lcfirst () |
Converts the first character of a string to lowercase. |
Levenshtein () |
Returns the Levenshtein distance between two strings. |
Localeconv () |
Returns local numeric and currency format information. |
LTrim () |
Removes white space characters or other characters to the left of a string. |
MD5 () |
Computes the MD5 hash of the string. |
Md5_file () |
Computes the MD5 hash of the file. |
Metaphone () |
Computes the Metaphone key for the string. |
Money_format () |
Returns a string formatted as a currency string. |
Nl_langinfo () |
Returns the specific local information. |
NL2BR () |
Inserts an HTML newline character before each new line in the string. |
Number_format () |
Format numbers in thousands. |
Ord () |
Returns the ASCII value of the first character in a string. |
Parse_str () |
Parses the query string into a variable. |
Print () |
Outputs one or more strings. |
printf () |
Outputs a formatted string. |
Quoted_printable_decode () |
Converts the quoted-printable string to a 8-bit string. |
Quoted_printable_encode () |
Converts a 8-bit string to a quoted-printable string. |
Quotemeta () |
The reference meta character. |
RTrim () |
Removes white space characters or other characters to the right of a string. |
SetLocale () |
Set up region information (geographic information). |
SHA1 () |
Computes the SHA-1 hash of the string. |
Sha1_file () |
Computes the SHA-1 hash of the file. |
Similar_text () |
Calculates the similarity of a two string. |
Soundex () |
Computes the Soundex key for the string. |
sprintf () |
Writes a formatted string to a variable. |
SSCANF () |
Parses input from a string according to the specified format. |
Str_getcsv () |
Parse the CSV string into the array. |
Str_ireplace () |
Replaces some of the characters in a string (insensitive to case). |
Str_pad () |
Fills the string with a new length. |
Str_repeat () |
Repeats the string to a specified number of times. |
Str_replace () |
Replaces some characters in a string (case sensitive). |
STR_ROT13 () |
Performs ROT13 encoding on the string. |
Str_shuffle () |
Randomly disrupts all characters in a string. |
Str_split () |
Splits a string into an array. |
Str_word_count () |
Calculates the number of words in a string. |
STRCASECMP () |
Compares two strings (not case sensitive). |
STRCHR () |
Finds the first occurrence of a string in another string. (The alias of the Strstr (). ) |
strcmp () |
Compares two strings (case sensitive). |
Strcoll () |
Compares two strings (based on local settings). |
STRCSPN () |
Returns the number of characters to look up in a string before any part of the specified character is found. |
Strip_tags () |
Strip the HTML and PHP tags from the string. |
Stripcslashes () |
Removes backslashes added by the addcslashes () function. |
Stripslashes () |
Removes backslashes added by the addslashes () function. |
Stripos () |
Returns the position of the first occurrence of a string in another string (not case sensitive). |
Stristr () |
Finds the position of the first occurrence of a string within another string (case insensitive). |
Strlen () |
Returns the length of the string. |
STRNATCASECMP () |
Use a "natural sort" algorithm to compare two strings (not case sensitive). |
STRNATCMP () |
Use a "natural sort" algorithm to compare two strings (case sensitive). |
STRNCASECMP () |
String comparison of the first n characters (not case sensitive). |
STRNCMP () |
A string comparison of the first n characters (case sensitive). |
STRPBRK () |
Finds any one character of a set of characters in a string. |
Strpos () |
Returns the position of the first occurrence of a string in another string (case sensitive). |
STRRCHR () |
The lookup string appears last in another string. |
Strrev () |
Reverses the string. |
Strripos () |
Finds the position of the last occurrence of a string in another string (not case sensitive). |
Strrpos () |
Finds the position of the last occurrence of a string in another string (case sensitive). |
STRSPN () |
Returns the number of specific characters contained in the string. |
Strstr () |
Finds the first occurrence of a string in another string (case sensitive). |
Strtok () |
Splits a string into smaller strings. |
Strtolower () |
Converts a string to a lowercase letter. |
Strtoupper () |
Converts a string to uppercase. |
STRTR () |
Converts a specific character in a string. |
SUBSTR () |
Returns a portion of a string. |
Substr_compare () |
Compares two strings from a specified starting position (binary security and case sensitivity). |
Substr_count () |
Calculates the number of occurrences of a substring in a string. |
Substr_replace () |
Replaces a part of a string with another string. |
Trim () |
Remove whitespace characters and other characters on either side of the string. |
Ucfirst () |
Converts the first character in a string to uppercase. |
Ucwords () |
Converts the first character of each word in a string to uppercase. |
vfprintf () |
Writes a formatted string to the specified output stream. |
vprintf () |
Outputs a formatted string. |
vsprintf () |
Writes the formatted string to the variable. |
WordWrap () |
Break string to a specified number of strings |
The strings in the table above can be understood!
PHP String Functions