Summary of common string functions in PHP

Source: Internet
Author: User
Tags array to string sprintf strtok

a function of judging type

Is_bool ()       //Determines whether the Boolean is_float ()      //Determines whether it is a floating-point is_real ()       ///Is_int ()/        /To determine whether the integer Is_integer ()    // Ibid. is_string ()     //Determines whether the string Is_object ()//Determines whether the object is Is_array ()//Determines whether      the array is_null ()       //Determines whether it is nullis_file ( )       //Determine if the file is Is_dir ()//Determine if it is a        directory is_numeric ()    //Determine if it is a digital is_nan ()//judgment is        not a digital is_resource ()   // Determine if the resource type is_a ($obj, $classname)//Determines whether the object is an instance of the class                      //Available if ($obj instanceof classname)

second, get the sub-string position


third, get the sub-string

substr ($str, $start [, $length]); Gets the substring substr_compare ($main _str, $str, $offset [, $length]); The substring comparison starts at offset and compares Substr_count ($HS, $nd [, $offset =0 [, $length]]); Gets the number of times the substring ND occurs in HS substr_replace ($string, $replacement, $start [, $length]);  String substring substitution//replace $string with $replacement strstr ($hys, $nd [, BOOL $before _needle from start, length of the substring = false]);//returns $ND where the first occurrence of $hys begins to the end of the string//third parameter if True returns $ND before the string stristr ($hys, $nd [, BOOL $before _needle = false]); Ibid., Ignoring case version strrchr ($hys, $nd); Returns the string $nd the last occurrence of the $hys to the end of the string//General and substr (STRRCHR ($hys, $nd), strlen ($nd)); Co-STRPBRK ($haystack, $char _list);//Match the entire string from $char_list to the first occurrence of a single character in $haystack//to the place where the string ends Strtok ( $str, $token);      For the first time, divide the string by delimiter $token strtok ($token); Second use eg. $string = "This Is\tan example\nstring";/* use tab and newline characters as the delimiter */$tok = Strtok ($string, "\n\t"); while ($tok!== fals e) {echo "word= $tok <br/>"; $tok = Strtok ("\n\t");}

Four, string str_ type function

Str_getcsv ($STR); Converts a CSV file string into an array of str_replace ($search, $replace, $subject [,& $count]);//Search and replace string           //Fourth parameter is specified, will be assigned to him the number of times Str_ireplace ($search, $replace, $subject [,& $count]);//Search and replace           the string//fourth parameter is specified, will be assigned to the number of times he replaced Ignores case str_shuffle (string $str);//randomly scrambles string str_split ($str [, $len = 1]);//convert string to an array                         //, each array cell has a length of $len

Five, string length

Strlen ($STR);  String length

six, flip string

Strrev (string $string);//Flip string

vii.. Mb_ type String function

The Mb_ type string is basically the same as the string function above,
Just add one more optional character encoding parameter, as in the usage
Some other useful functions are listed here

1. Character encoding of the detection string

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

VIII. related operations for strings1. Convert String type

Strval ($STR); Convert to String type Floatval ($STR);//Convert to floating-point intval ($STR); Convert to integral type

2, Case conversion

Strtolower ($STR); Convert all to lowercase strtoupper ($str); Convert all to Uppercase

3. String to Timestamp

Strtotime ($STR); Time format string converted to integer timestamp           //Note setting the time zone otherwise there will be a 8-hour error

4. Remove HTML and PHP tags

Strip_tags ($str [, $tags]);//Remove all labels not included in the $tags

5, ASCII to digital digital to ASCII

Chr (int $ascii); The number is converted to Asciiord (string $str); Returns the ASCII value of the first character of a $str

6. Encoding and decoding of JSON

Json_encode ($obj/$arr/$str ...); /encoded in JSON format string Json_decode ($jsonstr [, $assoc =true]); Decode to Object                         //return an array rather than an object when $assoc=true

7. Transfer <br/>

NL2BR ($STR); String $str Insert ' <br/> ' before all new lines

8. Array to string, string to array

Implode ($arr, $glue);//Convert one-dimensional array to string explode ();//String conversion to array

9, thousands of partition format

String Number_format (float $number [, int $decimals = 0]) string Number_format (float $number, int $decimals = 0, St Ring $dec _point = '. ', string $thousands _sep = ', ') @param   $number The number of digits you want to format $decimals the number of decimal           digits to retain $dec           _point specified Character displayed by decimal point           

10, go to space

Trim (String $str [, String $charlist]); Go to left and right character LTrim (string $str [, String $charlist]); Go left character RTrim (string $str [, String $charlist]); Go to right character

This function removes the white space character at the end of STR and returns it.

Without using the second argument, RTrim () removes only the following characters:
? "" (ASCII (0x20)), plain white space character.
? "\ T" (ASCII 9 (0x09)), tab.
? "\ n" (ASCII (0x0A)), line break.
? "\ R" (ASCII (0x0D)), carriage return.
? "\" (ASCII 0 (0x00)), NUL null byte character.
? "\x0b" (ASCII One (0x0B)), Vertical tab.
Filter characters can also be specified by the charlist parameter. In general, all the characters you want to filter are listed,
You can also use ".." To list a range of characters

11. Convert String encoding function

Iconv ($in _charset, $out _charset, $str); $in _charset input Character set $out_charset output character set

12. String encryption function

SHA1 ($STR); MD5 ($STR);

13. String escape and Inverse function

Addcslashes (String $str, String $charlist);//Escape special characters//eg in Strings               . Addcslashes ($str, "\0..\[email protected]\177..\ 377 ");   Escape characters in ASCII 0-37, 177-377 without the @ symbol stripcslashes ($str)-the inverse of the addcslashes () function escapes the processed string and  returns the literal string after the reversal. Can recognize \n,\r like C language, ... Description of octal and hexadecimal

14. Return data by Format

sprintf-returns the data as required, but does not output the following types of representations: string s integer d, u, C, O, X, x, b  Double G, G, E, E, F, f  eg. $num = 5; $locat Ion = ' tree '; $format = ' There is%d monkeys in the%s '; Echo sprintf ($format, $num, $location);





Summary of common string functions in PHP

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.