Summary of common PHP string functions

Source: Internet
Author: User
Tags check character strtok
Summary of common PHP string functions I. judgment 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
"; $ 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

Nl2br ($ str); // String $ str insert 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), common blank space character.
? "\ T" (ASCII 9 (0x09), tab.
? "\ N" (ASCII 10 (0x0A), line break.
? "\ R" (ASCII 13 (0x0D), carriage return.
? "\ 0" (ASCII 0 (0x00), NUL null byte.
? "\ 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 the escape function of the reverse addcslashes () function returns the string after the reverse definition. It can recognize \ n, \ r,... octal and hexadecimal descriptions similar to C.

14. return data by format

Sprintf? The data is returned as required, but no output can indicate the following types: 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.