Summary of common PHP string functions and php string Summary

Source: Internet
Author: User
Tags check character strtok

Summary of common PHP string functions and php string Summary
I. Judgment Type Functions

Is_bool () // determine whether it is a Boolean is_float () // determine whether it is a floating-point is_real () // same as is_int () // determine whether it is an integer is_integer () // same as is_string () // determine whether it is a string is_object () // determine whether it is an object is_array () // determine whether it is an array is_null () // determine whether it is nullis_file () // determine whether it is a file is_dir () // determine whether it is a directory is_numeric () // determine whether it is a number is_nan () // determine whether it is a resource type is_a ($ obj, $ classname) // determine whether the object is a class instance // if ($ obj instanceof Classname) is available)

Ii. Obtain the substring position

Strpos ($ hs, $ nd [, int $ offset = 0]) // returns the position of the number where the nd first appears in hs. Stripos ($ hs, $ nd [, int $ offset = 0]) // returns the position of the number that nd first appeared in hs, case insensitive. Strrpos ($ hs, $ nd [, int $ offset = 0]) // returns the last digit position of the nd in hs. Strripos ($ hs, $ nd [, int $ offset = 0]) // return the last digit position of the nd in hs, case insensitive.

3. Obtain substrings

Substr ($ str, $ start [, $ length]); // obtain the substring substr_compare ($ main_str, $ str, $ offset [, $ length]); // substring comparison starts from the offset to compare substr_count ($ hs, $ nd [, $ offset = 0 [, $ length]); // obtain the number of times that the sub-string nd appears in hs substr_replace ($ string, $ replacement, $ start [, $ length]); // replace string substrings // Replace $ replacement with the string strstr ($ hys, $ nd [, bool $ before_needle = false]); // returns the string starting from the first occurrence of $ hys to the end of the string. // if the third parameter is true, returns the string stris before $ nd. Tr ($ hys, $ nd [, bool $ before_needle = false]); // same as above, case-insensitive version strrchr ($ hys, $ nd) is ignored ); // returns the $ nd string starting from the last occurrence of $ hys to the end of the string // general and substr (strrchr ($ hys, $ nd ), strlen ($ nd); Use strpbrk ($ haystack, $ char_list ); // match the entire string strtok ($ str, $ token) where a single character appears for the first time in $ haystack from $ char_list ); // use strtok ($ token) to separate the string by $ token for the first time; // use eg for the second time. $ string = "This is \ tan example \ nstring";/* use a tab and line break as the delimiter */$ tok = strtok ($ s Tring, "\ n \ t"); while ($ tok! = False) {echo "Word = $ tok <br/>"; $ tok = strtok ("\ n \ t ");}

4. String str_type Functions

Str_getcsv ($ str); // convert the csv file string into an array str_replace ($ search, $ replace, $ subject [, & $ count]); // search and replace string // If the fourth parameter is specified, it will be assigned the number of replicas str_ireplace ($ search, $ replace, $ subject [, & $ count]); // If you specify the fourth parameter to search for and replace a string, it will be assigned the number of times it is replaced, regardless of the case (str_shuffle (string $ str ); // randomly disrupt the string str_split ($ str [, $ len = 1]); // convert the string into an array // with the length of each array unit being $ len

5. String Length

Strlen ($ str); // String Length

6. Flip a string

Strrev (string $ string); // flip string

7. mb _ type string functions

The mb _ type string is basically the same as the preceding string function,
Add an optional character encoding parameter. The usage is the same as above.
Some other useful functions are listed here.

1. Check character encoding of a string

$encode = mb_detect_encoding($lines, array("ASCII","UTF-8","GB2312","GBK","BIG5"));if($encode != "UTF-8"){    $lines = iconv($encode,"UTF-8", $lines);}

8. String-related operations 1. string type conversion

Strval ($ str); // convert to floatval ($ str) of the string type; // convert to float intval ($ str); // convert to integer

2. case-sensitive Conversion

Strtolower ($ str); // convert all to lower case strtoupper ($ str); // convert all to uppercase

3. string to Timestamp

Strtotime ($ str); // converts a string in the time format to an integer timestamp. // note that if you set the time zone, an error of 8 hours may occur.

4. Remove HTML and PHP tags

Strip_tags ($ str [, $ tags]); // remove all tags not included in $ tags

5. Convert ascii to numeric to ascii

Chr (int $ ascii); // converts a number to asciiord (string $ str); // returns the ascii value of the first character of $ str.

6. json encoding and decoding

Json_encode ($ obj/$ arr/$ str ...); // encoded as a json string json_decode ($ jsonstr [, $ assoc = true]); // decoded into an object // returns an array instead of an object when $ assoc = true

7. line feed <br/>

Nl2br ($ str); // string $ str Insert '<br/>' before all new lines'

8. convert an array to a string and convert a string to an array.

Implode ($ arr, $ glue); // convert a one-dimensional array to a string explode (); // convert a string to an array

9. Split and format thousands of BITs

String number_format (float $ number [, int $ decimals = 0]) string number_format (float $ number, int $ decimals = 0, string $ dec_point = '. ', string $ thousands_sep = ',') @ param $ number the number you want to format $ decimals the number of decimal places to be retained $ dec_point specifies the characters to display the decimal point $ thousands_sep specifies the characters to display by the thousands Separator

10. Remove Space

Trim (string $ str [, string $ charlist]); // remove the left and right characters ltrim (string $ str [, string $ charlist]); // remove the Left character rtrim (string $ str [, string $ charlist]); // remove the right character

This function deletes the white space characters at the end of str and returns the result.

Without the second parameter, rtrim () only deletes the following characters:
• "" (ASCII 32 (0x20), a regular blank space.
• "\ T" (ASCII 9 (0x09), Tab.
• "\ N" (ASCII 10 (0x0A), line feed.
• "\ R" (ASCII 13 (0x0D), carriage return.
• "\ 0" (ASCII 0 (0x00), NUL Null Byte Character.
• "\ X0B" (ASCII 11 (0x0B), vertical tab.
Filter characters can also be specified by the charlist parameter. It is generally necessary to list all characters to be filtered,
You can also use ".." to list a character range.

11. Convert string encoding Functions

Iconv ($ in_charset, $ out_charset, $ str); $ in_charset input character set $ out_charset output character set

12. String encryption functions

sha1($str); md5($str);

13. String escape and inverse Functions

Addcslashes (string $ str, string $ charlist); // special character in the escape string // eg. addcslashes ($ str, "\ 0 .. \ 37! @ \ 177 .. \ 377 "); // escape the stripcslashes ($ str) characters without the @ symbol in ascii 0-37 and 177-377 () the string processed by function escaping returns the string after the inverse meaning. It can recognize \ n, \ r,... octal and hexadecimal descriptions similar to C.

14. return data by format

Sprintf-return the data as required, but the data type can be expressed as follows: string s integer d, u, c, o, x, X, B double g, G, e, e, f, F eg. $ num = 5; $ location = 'tree'; $ format = 'there are % d monkeys in the % s'; echo sprintf ($ format, $ num, $ location );





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.