PHP string functions

Source: Internet
Author: User
Tags chop crypt ereg imap strtok
PHP string functions

Addslashes: adds a slash to the string.
Bin2hex: Binary to hexadecimal.
Chop: removes consecutive gaps.
CHR: returns the character of the ordinal value.
Chunk_split: Splits a string into segments.
Convert_cyr_string: Convert the ancient Slavic string to another string.
Crypt: encrypt the string with DES encoding.
Echo: Output string.
Explode: Cut string.
Flush: clears the output buffer.
Get_meta_tags: extracts all metadata marked by Meta from the file.
Htmlspecialchars: convert special characters into HTML format.
Htmlentities: converts all characters into HTML strings.
Implode: converts an array into a string.
Join: converts an array into a string.
Ltrim: removes consecutive white spaces.
MD5: Calculate the MD5 of the string.
Nl2br: converts a line break to <br>.
Ord: returns the ordinal value of a character.
Parse_str: parses the query string into a variable.
Print: Output string.
Printf: output the formatted string.
Quoted_printable_decode: convert an QP encoded string to an 8-Bit String.
Quotemeta: Add a reference symbol.
Rawurldecode: Restores a string from a URL-specific format to a normal string.
Rawurlencode: encode a string into a URL-specific format.
Setlocale: configure the region information.
Similar_text: String similarity calculation.
Soundex: calculates the pronunciation of a string.
Sprintf: format the string.
Strchr: Find the first occurrence character.
Strcmp: String comparison.
Strcspns: the length of different strings.
Strip_tags: removes HTML and PHP tags.
Stripslashes: removes the backslash.
Strlen: gets the string length.
Strrpos: Find the last occurrence of a character in the string.
Strpos: searches for the first occurrence of a character in a string.
Strrchr: gets the string starting from the last occurrence of a character.
Strrev: reverse string.
Strspns: identifies the number of masks that a string falls into another string.
Strstr: returns the string from the beginning to the end of a string.
Strtok: Cut string.
Strtolower: converts all strings to lowercase letters.
Strtoupper: converts all strings to uppercase.
Str_replace: String replacement.
Strtr: converts certain characters.
Substr: obtains some strings.
Trim: removes spaces at the beginning and end of a string.
Ucfirst: uppercase for the first character of the string.
Ucwords: change the first letter of each character in the string to uppercase.
Return Value: String
Function Type: Data Processing
Description: This function allows the database to process strings with diagonal lines added to the quotation marks for the database query to operate smoothly. The modified characters include single quotation marks ('), double quotation marks ("), backslash (\), and null character NUL (the Null Byte ).
Reference: stripslashes () htmlspecialchars () quotemeta ()
Return Value: String
Function Type: Data Processing
Description: This function converts a binary string to a hexadecimal string.
Example
Supplement:
The josh@superfork.com (22-jun-1999) writes a function that converts a hexadecimal character into a binary character.
<?
Function hex2bin ($ data ){
$ Len = strlen ($ data );
For ($ I = 0; $ I & lt; $ Len; $ I + = 2 ){
$ Newdata. = pack ("C", hexdec (substr ($ string, $ I, 2 )));
}
Return $ newdata;
}
?>
Return Value: String
Function Type: Data Processing
Description: This function clears consecutive blank spaces of strings.
Example
<?
$ Trimmed = chop ($ line );
?>
Reference: trim ()
Return Value: String
Function Type: Data Processing
Description: This function converts the ordinal number of characters into ASCII characters. This function is compared with ord.
Example
<?
$ Str. = CHR (27 );
$ STR = sprintf ("the end character of the string is % C", 27 );
?>
Reference: ord () sprintf ()
Return Value: String
Function Type: Data Processing
Description: This function converts characters into short segments for other functions. For example, base64_encode. It is determined that the chunklen parameter (76 characters) is inserted with end ("\ r \ n") every 76 characters "). Returns a new string without modifying the original string.
Example
Format String $ data into mime base64 format
<?
$ New_string = chunk_split (base64_encode ($ data ));
?>
Reference: ereg_replace ()
Return Value: String
Function Type: Data Processing
Content Description: This function converts the ancient Slavic string to another string. The from and to parameters are characters and their meanings are as follows:
K-koi8-r
Ws-windows-1251
Iso8859-5
A x-cp866
D x-cp866
M-X-Mac-Cyrillic
Syntax: String crypt (string STR, string [salt]);
Return Value: String
Function Type: Encoding
Description: This function encrypts the string with the standard des module of UNIX. This is a one-way encryption function and cannot be decrypted. To compare the string, place the first two characters of the encrypted string in the salt parameter, and then compare the encrypted string.
For more information, see crypt in Unix manual (man.
In some newer UNIX versions, apart from Des, other encryption modules, such as MD5, are provided. Even some systems use MD5 instead of DES. There are still some changes to the salt parameter, depending on the length of the string passed to the salt parameter:
Crypt_std_des-standard des encoding, which is a 2-character salt.
Crypt_ext_des-extended des encoding, which is a 9-character salt.
Crypt_md5-MD5 encoding, input 12 characters plus $1 $ salt.
Crypt_blowfish-extended des encoding. Input 16 characters plus $2 $ salt.
In addition, if the salt parameter is not used, the program will automatically generate it.
Return Value: None
Function types: PhP system functions
Description: This function outputs strings. Because it is not a real function, there is no return value.
Example
<? PHP
Echo "Hello World ";
?>
Reference: Print () printf () flush ()
Returned value: Array
Function Type: Data Processing
Description: This function is used to cut the string according to the specified string or character separator. Returns the cut string to the array variable.
Example
<?
$ Pizza = "first, second, third, fourth, fifth, and sixth ";
$ Pieces = explode ("", $ pizza );
?>
Reference: implode ()
Return Value: None
Function Type: Data Processing
Description: This function has no input or output. Send and clear data in the output buffer.
Syntax: array get_meta_tags (string filename, int [use_include_path]);
Returned value: Array
Function Type: Data Processing
Description: This function extracts all the data marked by <meta...> In the homepage and places the array variable to return. For example
<HTML>
<Head>
<Meta content = "Peng Wuxing">
<Meta content = "php Bible">
<Title> PHP Bible </title>
</Head> <! -- This function only ends here -->
This function finds out the meta tag. The name attribute string is an array index, while the content attribute string is the content of the array. Note that this function is a PHP native function. On UNIX platforms, you cannot directly process the file format of the Macintosh tower because the line breaks are different. The blake@mediaone.net points out (07-jun-1999) that the filename parameter can also be a URL and the function will extract the file meta tag from the remote server. When the function is processing, the Syntax: String htmlspecialchars (string );
Return Value: String
Function Type: Data Processing
Description: This function converts special characters into HTML strings (&....;). The most common use case may be the message board for handling customer messages.
& (And) &
"(Double quotation marks)"
<(Less than) to & lt;
> (Greater than) to & gt;
This function only converts the preceding special characters and does not convert all of them into the ASCII conversion defined in HTML.
Example
This is an example provided by aulbach@unter.franken.de (27-Jan-1999)
<Form action = bla>
<H2> restaurant description <H2>
Name of restaurant:
<Input type = text value = "& lt ;?
Echo htmlspecialchars ($ restname);?> "& Gt;
<! -- The variable $ restname is $ restname = "\" the white horse \ ""; -->
<Br>
Enter the description (HTML can be used directly): <br>
<Textarea> <?
Echo htmlspecialchars ($ descript );
?> </Textarea>
<Input type = submit>
</Form>
Reference: htmlentities () nl2br ()
Syntax: String htmlentities (string );
Return Value: String
Function Type: Data Processing
Content Description: This function is a bit like the htmlspecialchars () function, but this function converts all string characters into Special Character Set strings in HTML. However, there will be a lot of troubles in reading the source code of the webpage after conversion, especially when the text in the source code of the webpage becomes invisible to the cloud, and what the browser sees is still normal.
Return Value: String
Function Type: Data Processing
Description: This function combines the content of the array into a string. The glue parameter is the delimiter between words.
Example
<?
$ Colon_separated = implode (":", $ array );
Echo $ colon_separated;
?>
Reference: explode () join () Split ()
Return Value: String
Function Type: Data Processing
Description: This function is the alias of the implode function.
Return Value: String
Function Type: Data Processing
Description: This function is used to delete consecutive white spaces in a string ).
Syntax: String MD5 (string Str );
Return Value: String
Function Type: Encoding
Content Description: This function is used to calculate MD5. For details about the MD5 encoding method, refer to RSA Data Security, Inc. MD5 message-Digest algorithm. rfc1321; or modern cryptography and its applications, such as laxisong, Han Liang, and Zhang sincerely, 11.3.
Syntax: String nl2br (string );
Return Value: String
Function Type: Data Processing
Description: This function converts a line feed character to an HTML line feed <br> instruction.
Reference: htmlspecialchars () htmlentities ()
Return Value: integer
Function Type: Data Processing
Description: This function returns the ASCII (American National Standard Exchange Code) Sequence Value of the character. This function is opposite to the CHR () function.
Example
<? PHP
If (ord ($ Str) = 10 ){
Echo ("the first character of string \ $ STR is a line break character. \ N ");
}
?>
Reference: CHR ()
Syntax: void parse_str (string Str );
Return Value: None
Function Type: Data Processing
Description: This function parses the QUERY_STRING string of the get method returned by the browser. The returned variable name and value are based on the name and value of QUERY_STRING.
Example
<? PHP
$ STR = "first = Value & Second [] = This + works & Second [] = another ";
Parse_str ($ Str );
Echo $ first; // The "value" string is displayed.
Echo $ second [0]; // display the "this works" String
Echo $ second [1]; // display the "another" String
?>
Return Value: Boolean
Function Type: Data Processing
Description: The output string of this function. If the call succeeds, 1 is returned. If the call fails, 0 is returned. For example, if the client's browser suddenly fails during transmission, the output may fail.
Reference: ECHO () printf () flush ()
Return Value: integer
Function Type: Data Processing
Description: This function formats strings according to the content format specified by the parameter format. For details about the format, see sprintf ().
Reference: Print () sprintf () flush ()
Syntax: String quoted_printable_decode (string Str );
Return Value: String
Function Type: Encoding
Description: This function decodes the quoted-printable string into an 8-bit encoded string. This function is similar to the imap_qprint () function. The only difference is that the imap_qprint () function requires the system to be added to the IMAP module, and this function does not need the IMAP module.
Return Value: String
Function Type: Data Processing
Description: This function will contain. \ + *? [^] ($) And other characters are preceded by the Backslash "\" symbol.
Reference: addslashes () htmlentities () htmlspecialchars () nl2br () stripslashes ()
Syntax: String rawurldecode (string Str );
Return Value: String
Function Type: Encoding
Description: This function decodes strings. Parses the string from the special format of the URL into a normal string. For detailed encoding and decoding information and specification files, refer to RFC 1738.
Reference: rawurlencode ()
Syntax: String rawurldecode (string Str );
Return Value: String
Function Type: Encoding
Description: This function encodes a string into a special URL string format. special characters are converted into a percent sign followed by two Sixteen digits. For example, the space is changed to % 20.
Example
<? PHP
Echo '& lt; a href = "ftp: // Guest:', rawurlencode ('foo @ + %/'),' @ localhost/x.txt"> ';
? & Gt;
Reference: rawurldecode ()

Return Value: String
Function types: operating system and environment
Description: This function is used to configure the region information. The category parameter has the following options:
Lc_all includes all the following options.
The lc_collate configuration string is compared. php has not implemented this item yet.
Lc_ctype: Specifies the character category and conversion. For example, change all to uppercase strtoupper ().
Lc_monetary: configure the financial currency. php has not yet been implemented.
Lc_numeric: specifies the number of digits after the decimal point.
Lc_time is used in combination with strftime.
If the parameter locate is an empty string "", the system environment variable locate or Lang value is used. If the locate value is zero, the local configuration will not be changed. Returns a new region. If the system has not yet implemented the region, false is returned.
Example
Michal FITA presented this land-based Polish example at 11-Jan-1999.
<?
Setlocale ("lc_all", "PL ");
$ Net = "1234,56 ";
$ Gross = "1,22" * $ net;
Printf ("gross profit: % s, net profit: % s", $ gross, $ net ");
/* Total Poland? /Font> [the value of tax is 22% */
? & Gt;
The return value is
Gross profit:, 56, net profit :,
Return Value: integer
Function Type: Data Processing
Description
This function is used to calculate the degree of similarity between two strings.
Return Value: String
Function Type: Data Processing
Description of content: the soundex value is the value obtained by the approximate English pronunciation. The value consists of four characters. The first character is an English letter, and the last three are digits. In pinyin text, you may sometimes read but cannot spell out the correct words. In particular, you can use this function to perform a fuzzy comparison similar to that of a search engine. For example, the soundex values of knuth and Kant are both h416. For more details, refer to the masterpiece of Donald knuth: The art of computer programming (the art of computer programming.
Example
<?
$ Str1 = soundex ("Wilson ");
$ Str2 = soundex ("waillsume ");
Echo "soundex (\" Wilson \ ") = $ str1 equals \ n ";
Echo "soundex (\" waillsume \ ") = $ str2 \ n ";
Echo "values are $ str1 ";
?>
The string returned in the preceding example is
Soundex ("Wilson") = w425 equals
Soundex ("waillsume") = w425
All values are w425
Return Value: String
Function Type: Data Processing
Description: This function is used to format strings. The format parameter is the conversion format, starting with the percent sign % to the conversion character. The format of the conversion includes
Fill in the blanks. If the value is 0, it indicates that the space is set to 0; if the value is 0, it indicates that the space is placed.
Alignment. The default value is right-aligned, and the negative number table is left-aligned.
Field width. Minimum width.
Precision. The number of digits after the decimal point.
Type, as shown in the following table %. Do not convert.
The integer B is converted into binary.
The C integer is converted to the corresponding ASCII character.
D integer to decimal place.
F times the precision number to the floating point number.
O integer to octal.
The S integer is converted into a string.
Convert an integer to a lowercase hexadecimal value.
Convert X to uppercase hexadecimal.
Example
<?
$ Money1 = 68.75;
$ Money2 = 54.35;
$ Money = $ money1 + $ money2;
// At this time, the variable $ money value is "123.1 ";
$ Formatted = sprintf ("% 01.2f", $ money );
// At this time, the variable $ formatted value is "123.10"
?>
Return Value: String
Function Type: Data Processing
Description: This function is the strstr () function.
Return Value: integer
Function Type: Data Processing
Description: This function is used to compare the size of a binary string. Returns a negative number indicating that str1 is less than str2; returns a positive number indicating that str1 is greater than str2; returns a zero value indicating that the two strings are the same.
Reference: ereg () substr () strstr ()
Return Value: integer
Function Type: Data Processing
Description: This function is used to compare two strings and calculate the length of different strings.
Reference: strspns ()
Syntax: String strip_tags (string Str );
Return Value: String
Function Type: Data Processing
Description: This function removes any HTML and PHP tag strings contained in the string. If the HTML and PHP tags of a string are incorrect, for example, if a greater symbol is missing, an error is returned. This function and fgetss () have the same functions.
Return Value: String
Function Type: Data Processing
Description: This function removes the backslash character from the string. If there are two backslash lines in a row, remove one and leave one. If there is only one backslash, remove it directly.
Reference: addslashes ()
Return Value: integer
Function Type: Data Processing
Description: This function returns the specified String Length.
Return Value: integer
Function Type: Data Processing
Description: This function is used to find the final position of the character needle in the haystack string. It is worth noting that the needle can only be one character, and it is not suitable for Chinese characters. If the specified character is not found, false is returned.
Reference: strpos () strrchr () substr () strstr ()
Return Value: integer
Function Type: Data Processing
Description: This function is used to find the first position where the character needle in the haystack string appears. It is worth noting that the needle can only be one character, and it is not suitable for Chinese characters. If the specified character is not found, false is returned. The offset parameter can be omitted, which is used to start searching from offset.
Reference: strrpos () strrchr () substr () strstr ()
Return Value: integer
Function Type: Data Processing
Description: This function is used to find the final position of the character needle in the string haystack and return the string between the position and the end of the string haystack. If no needle is found, false is returned.
Example
Next, retrieve the last path of the Environment Variable path.
<? PHP
$ Dir = substr (strrchr ($ path, ":"), 1 );
Echo "the last path is:". $ dir;
?>
Reference: substr () strstr ()
Return Value: String
Function Type: Data Processing
Description: reverse the character string.
Example
The return string of the following example is "GNEP nosliw"
<?
$ STR = strrev ("Wilson peng ");
Echo $ STR;
?>
Return Value: integer
Function Type: Data Processing
Description: This function uses the str2 string as the mask and can be used to calculate several characters in the str1 string that fall into the str2 mask.
Reference: strcspn ()
Return Value: String
Function Type: Data Processing
Description: This function returns the string from haystack to the end of haystack when needle first appears. If no needle is found, false is returned.
Reference: strrchr () substr () ereg ()
Return Value: String
Function Type: Data Processing
Description: This function splits string arg1 into short strings based on the value of string arg2.
Example: In this example, the I will be back string is left blank.
<? PHP
$ String = "I will be back ";
$ Tok = strtok ($ string ,"");
While ($ Tok ){
Echo "single word = $ Tok & lt; br> ";
$ Tok = strtok ("");
}
? & Gt;
Reference: Split () explode ()
Return Value: String
Function Type: Data Processing
Description: This function reduces all STR strings and writes them into strings.
Reference: strtoupper () ucfirst ()
Return Value: String
Function Type: Data Processing
Description: This function converts all STR strings to uppercase strings.
Reference: strtolower () ucfirst ()
Return Value: String
Function Type: Data Processing
Description: This function substitutes STR into the haystack string and replaces all needle with Str. Mlevine@adtraq.com (11-apr-1999) pointed out that in PHP 3.0.7, this function has some bugs, and the nadeem@bleh.org (05-jun-1999) supplement in PHP 3.0.8 version function will return to normal.
Example
Replace % body % with black in the following example
<? PHP
$ Bodytag = str_replace ("% body %", "black", "& lt; body text = % body %> ");
Echo $ bodytag;
? & Gt;
Reference: ereg_replace ()
Return Value: String
Function Type: Data Processing
Description: This function converts the characters in the STR string and from into to characters.
Reference: ereg_replace ()
Return Value: String
Function Type: Data Processing
Description: This function extracts the length from the start character of the string. If start is a negative number, it is counted from the end of the string. If the parameter length can be omitted, but it is a negative number, it indicates that the maximum length is obtained.
Example
<?
Echo substr ("abcdef", 1, 3); // return "BCD"
Echo substr ("abcdef",-2); // return "Ef"
Echo substr ("abcdef",-3, 1); // return "D"
Echo substr ("abcdef", 1,-1); // return "bcde"
?>
Reference: strrchr () ereg ()
Return Value: String
Function Type: Data Processing
Description: This function returns the string removed from the white space at the beginning and end of the string.
Refer to: Chop () ltrim ()
Return Value: String
Function Type: Data Processing
Description: This function returns the first letter of the string 'str' in uppercase.
Reference: strtoupper () strtolower ()
Return Value: String
Function Type: Data Processing
Description: This function returns the string 'str'. The first letter and letter of each word are all converted to uppercase letters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.