PHP string Common functions

Source: Internet
Author: User
Tags explode ord rtrim strtok

arrays, strings, and databases are the most, most, and most commonly used functions in our functions, arrays and databases We haven't talked about it yet, and we'll come back to you when we talk about it.

Of course, there are many string functions in PHP. The two series of strings we use most often:

1. Single byte string processing function

2. Multibyte string processing functions

3. String encoding conversion function

Let's say why we need to learn so many functions:

1. We are learning Chinese, which is double-byte or three-byte. The foreigner's function can only handle English and the number of these single-byte string processing does not bird Chinese. Not up to our functional requirements

2. Sometimes it is necessary to do the conversion between different character encodings, for example: converting GBK to UTF-8

3. English these characters must be dealt with in the computer.

PHP Common functions

The main use of Chinese is GBK and utf-8 two encoding format.
GBK and Utf-8 are two different coding boards that encode the Chinese characters.

They stipulate that GBK is double-byte, that is, a Chinese character occupies 2Bytes.
The Utf-8 is three bytes, and a Chinese character occupies three bytes of storage space.

Name of function Description Example
Trim () Remove spaces or other predefined characters at each end of a string "$str =" \r\nhello world!\r\n "; echo Trim ($STR);
RTrim () Remove whitespace or other predefined characters to the right of a string "$str =" Hello world!\n\n "; echo RTrim ($STR); "
Chop () Alias for RTrim () Ditto
LTrim () Remove spaces or other predefined characters to the left of a string "$str =" \r\nhello world! "; Echo LTrim ($STR); "
DirName () Back to the directory part of the path (we put it in the string function) Echo dirname ("c:/testweb/home.php");
Str_pad () Fills a string with the specified length $STR = "Hello World"; Echo Str_pad ($str, 20, ".");
Str_repeat () Reusing the specified string Echo str_repeat (".", 13);
Str_split () To split a string into an array Print_r (Str_split ("Hello"));
Strrev () Invert string Echo Strrev ("Hello world!");
WordWrap () Wrapping a string by a specified length "$STR =" "An example on a long word is:supercalifragulistic" "; Echo WordWrap ($STR, 15); "
Str_shuffle () Randomly disrupts all characters in a string Echo str_shuffle ("Hello World");
Parse_str () Parsing a string into a variable "Parse_str (" Id=23&name=john%20adams ", $myArray); Print_r ($myArray); "
Number_format () Formatting numbers with thousands of groupings "Echo Number_format (" 1000000 "); Echo Number_format ("1000000", 2); Echo Number_format ("1000000", 2, "", "", "". ");"
Strtolower () String converted to lowercase Echo strtolower ("Hello world!");
Strtoupper () String to uppercase Echo strtoupper ("Hello world!");
Ucfirst () Capitalize the first letter of the string echo ucfirst ("Hello World");
Ucwords () String each word first character to uppercase echo ucwords ("Hello World");
Htmlentities () Convert a character to an HTML entity $str = "John & ' Adams '"; Echo htmlentities ($str, Ent_compat);
Htmlspecialchars () Predefined word representable HTML encoding
NL2BR () \ n escaped as
Label
Echo nl2br ("One line.\nanother line.");
Strip_tags () Strip HTML, XML, and PHP tags Echo strip_tags ("Hello world!");
Addcslashes () Adds a backslash to an escaped character in a string before the specified character $str = "" Hello, my name is John Adams. "Echo $str; Echo addcslashes ($str, ' m '); "
Stripcslashes () Remove the backslash added by Addcslashes () Echo stripcslashes ("Hello, \my Na\me is Kai ji\m.");
Addslashes () To add a backslash before specifying a predefined character $str = "Who ' s John Adams?"; echo addslashes ($STR);
Stripslashes () Remove the escape character added by Addslashes () Echo stripslashes ("who\ ' s John Adams?");
Quotemeta () Add a backslash before certain predefined characters in the string $STR = "Hello World". (Can you hear me?) "; echo Quotemeta ($STR);
Chr () Returns the character from the specified ASCII value Echo Chr (052);
Ord () Returns the ASCII value of the first character of a string echo ord ("Hello");
STRCASECMP () Case-insensitive comparison of two strings echo strcasecmp ("Hello world!", "Hello world!");
strcmp () Case-sensitive comparison of two strings
STRNCMP () Compares the first n characters of a string, case sensitive
STRNCASECMP () Compares the first n characters of a string, not case-sensitive int strncasecmp (String $str 1, String $str 2, int $len)
STRNATCMP () Natural order method compares string length, case sensitive int strnatcmp (String $str 1, String $str 2)
STRNATCASECMP () Natural order method compares string length, case-insensitive int strnatcasecmp (String $str 1, String $str 2)
Chunk_split () Divide a string into small chunks Str chunk_split (str $body [, int $len [, str $end]]
Strtok () Cut string Str strtok (str $str, str $token)
Explode () Use one string to split another string for a flag Array explode (str $sep, str $str [, int $limit])
Implode () Join, concatenate the array values with the subscription character into a string String implode (string $glue, array $pieces)
SUBSTR () Intercept string String substr (string $string, int $start [, int $length])
Str_replace () String substitution operations, case-sensitive Mix str_replace (Mix $search, Mix $replace, Mix $subject [, int & $num])
Str_ireplace () String substitution operation, case insensitive Mix str_ireplace (Mix $search, Mix $replace, Mix $subject [, int & $count])
Substr_count () Counts the number of occurrences of a string in another string int Substr_count (string $haystack, string $needle [, int $offset = 0 [, int $length]])
Substr_replace () Replace a string in another string Mixed Substr_replace (mixed $string, string $replacement, int $start [, int $length])
Similar_text () Returns the number of identical characters in two strings int Similar_text (str $str 1,str $str 2)
STRCHR () Returns a string in another string starting at the end of a string String Strstr (String $str, String $needle, bool $before _needle)
STRRCHR () Returns a string in another string where the last occurrence of the position begins at the end of the string String STRRCHR (String $haystack, mixed $needle)
Stristr () Returns a string in another string starting at the end of the string, not case-sensitive String Stristr (String $haystack, mixed $needle [, bool $before _needle = false])
STRTR () Convert some characters in a string String Strtr (String $str, String $from, String $to)
Strpos () Finding where a character in a string first appears int Strpos (string $haystack, mixed $needle [, int $offset = 0])
Stripos () Finds the position of a character in a string that appears first, not case-sensitive int Stripos (string $haystack, string $needle [, int $offset])
Strrpos () Find the last occurrence of a character in a string int Strrpos (string $haystack, string $needle [, int $offset = 0])
Strripos () Finds the last occurrence of a character in a string, not case-sensitive int Strripos (string $haystack, string $needle [, int $offset])
STRSPN () Returns the length of a substring in a string that first matches mask int strspn (String $str 1, String $str 2 [, int $start [, int $length]])
STRCSPN () Returns the length of a string in a string that does not match mask int strcspn (String $str 1, String $str 2 [, int $start [, int $length]])
Str_word_count () Statistics string contains the number of words Mix str_word_count (str $str, [])
Strlen () Statistical string length int strlen (str $str)
Count_chars () Number of occurrences of all letters in the statistics string (0..255) Mixed Count_chars (string $string [, int $mode])
MD5 () String MD5 encoding $str = "Hello"; echo MD5 ($STR)
Iconv
Mb_substr Gets the part of the string String Mb_substr (string $str, int $start [, int $length = NULL [, String $encoding = Mb_internal_encoding ()]])
Mb_http_output Set/Get HTTP output character encoding Mixed Mb_http_output ([string $encoding = Mb_http_output ()])
Mb_strlen Gets the length of the string Mixed Mb_strlen (String $str [, String $encoding = Mb_internal_encoding ()])
Iconv Strings are converted as required character encodings String Iconv (String $in _charset, String $out _charset, String $str)
Iconv_substr section that intercepts a string
Iconv_get_encoding Get internal configuration variables for the iconv extension
Mb_substr_count Count the number of occurrences of a string
Mb_check_encoding Checks if the string is valid in the specified encoding
Mb_strrpos Find where the string last appears in a string
Mb_split Using regular expressions to split multibyte strings
Parse_url Interpret the URL as an array

PHP string Common functions

Related Article

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.