Summary of common PHP string operation function instances (trim, nl2br, addcslashes, uudecode, md5, etc)

Source: Internet
Author: User
This article mainly introduces common string operation functions in PHP, and summarizes and analyzes common PHP skills for string operation in combination with examples, which has some reference value, you can refer to the examples in this article to summarize common PHP string operation functions. We will share this with you for your reference. The details are as follows:

/* Common string output function ** echo () output string * print () output one or more strings * die () output one message, and exit the current script * printf () output the formatted string * sprintf () to write the formatted string to a variable ** // ucfirst // convert the first letter of the string to uppercase $ str = "string "; echo ucfirst ($ str); echo"
"; // Ucwords () // uppercase the first letter of each word in the string $ ucword =" hello everyone! "; Echo ucwords ($ ucword); echo"
"; // Ltrim () rtrim () trim () // Remove space $ str =" 123 This is a test ..... "; echo ltrim ($ str," 0 .. 9 ")."
"; // Remove the number echo rtrim ($ str,". ") on the left ,".")."
"; Echo trim ($ str," 0 .. 9A .. Z .")."
"; // Remove uppercase letters at both ends of the string, numbers, and. // HTML-related string formatting functions // nl2br () // Convert \ n in the string"
"$ Str =" this is \ n hello world "; echo nl2br ($ str ).'
'; // Htmlspecialchars () // Mark the html as a character, not interpreted $ str ="Hello world"; Echo $ str ."
"; Echo htmlspecialchars ($ str); echo"
"; // Addcslashes // add A backslash $ str = addcslashes (" foo [] "," A.. z "); echo $ str ."
"; Echo addcslashes (" zoo ['.'] ", 'A.. z ')."
"; // Convert_uuencode () // use uudecode to encode the string $ string =" hello world "; $ str = convert_uuencode ($ string); echo $ str ."
"; Echo convert_uudecode ($ str )."
"; // Html_entity_decode (string $ string [, int $ flags = ENT_COMPAT | ENT_HTML401 [, string $ encoding = 'utf-8']) // opposite to htmlentities, convert the encoded html characters into the form that the browser can compile $ a = "I want a brightFuture"; $ B = htmlentities ($ )."
"; Echo $ B; echo html_entity_decode ($ B); echo"
"; // Htmlspecialchars_decode (string $ string [, int $ flags = ENT_COMPAT | ENT_HTML401]) // opposite to the htmlspecialchars function, convert an HTML object to the character $ c = htmlspecialchars ($ a); echo $ c."
"; Echo htmlspecialchars_decode ($ c )."
"; Echo"
"; // Lcfirst (string $ str) // lowercase $ str =" Hello World "; // echo lcfirst ($ str )."
"; // Md5_file (string $ filename [, bool $ raw_output = false]) // perform md5 encryption on the file // $ string =" password "; $ str = md5 ($ string); if ($ str = "5f4dcc3b5aa765d61d8327deb882cf99") {echo "The password is right
";}// Parse_str (string $ str [, array & $ arr]) // parses a string, parsed into variables and arrays $ str = "first = value & arr [] = foo + bar & arr [] = baz"; parse_str ($ str, $ input ); print_r ($ input); echo"
"; // String sha1_file (string $ filename [, bool $ raw_output = false]) // calculate the file's hash value foreach (glob (" C: /lamp/appache2/htdocs /*. php ") as $ ent) {if (is_dir ($ ent) {continue;} echo $ ent. "(SHA1 :". sha1_file ($ ent ). ")
";} Echo"
"; // Int similar_text (string $ first, string $ second [, float & $ percent]) // calculate the similarity between the two strings and pass the third parameter through reference, similar_text () calculates the percentage of similarity. $ String1 = "rogerzhalili"; $ string2 = "zhangjieroger"; if (similar_text ($ string1, $ string2, $ percent) {echo $ string1. "and ". $ string2. "has the similarity :". $ percent."
";} Echo"
"; // String str_shuffle (string $ str) // disrupt a string $ string =" I want you to solve this problem "; echo str_shuffle ($ string )."
"; // Array str_split (string $ string [, int $ split_length = 1]) // splits the string according to the specified length $ arr = str_split ($ string, 3 ); // str_word_count (string $ string [, int $ format = 0 [, string $ charlist]) // count the number of words in the string echo"
"; // Int strripos (string $ haystack, string $ needle [, int $ offset = 0]) // searches for the last occurrence of a specified string in the target string in case insensitive. Unlike strrpos (), strripos () is case insensitive. // Offset is used to specify the position from which to start searching for $ haystack = 'ababcd'; $ needle = 'AB'; echo "the last ". $ needle. "postion is :". strripos ($ haystack, $ needle )."
"; Echo strrpos ($ haystack, 'AB'); echo"
"; // String strstr (string $ haystack, mixed $ needle [, bool $ before_needle = false]) // returns the haystack string from the position where needle first appeared to the end of the haystack knot. This function is case sensitive. If you want to be case insensitive, use // stristr (). $ A = "the First test"; $ needle = "Fi"; echo strstr ($ a, $ needle )."
"; If ($ c = strstr ($ a," Fio ") {echo" find ". $ c ."
";} Else {echo" not find the string!
";} Echo"
"; // Int substr_count (string $ haystack, string $ needle [, int $ offset = 0 [, int $ length]) // Find the number of times the $ needle substring appears in $ haystack. $ needle is case sensitive $ hay = "la wa lala"; echo substr_count ($ hay, "la ")."
"; // Int preg_match_all (string $ pattern, string $ subject [, array & $ matches [, int $ flags = PREG_PATTERN_ORDER [, int $ offset = 0]) // regex-match: store the matched results to $ matches (if $ matches is specified) preg_match_all ("/? (\ D3 )?? (? (1) [\-\ s]) \ d {3}-\ d {4}/x "," Call 555-1212 or 1-800-555-1212 ", $ phones); echo"
";print_r($phones);echo "
"; Echo"
"; // Preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count]) // search for the part in the subject that matches pattern and replace it with replacement. $ string = '10000l 15,200 3'; $ pattern = '/(\ w +) (\ d +), (\ d +)/I '; $ replacement = '$ {1} 1, $ 3'; echo preg_replace ($ pattern, $ replacement, $ string); echo"
"; // Array preg_split (string $ pattern, string $ subject [, int $ limit =-1 [, int $ flags = 0]) // use a regular expression to separate a given string. $ str = 'string'; $ chars = preg_split ('//', $ str,-1, PREG_SPLIT_NO_EMPTY); print_r ($ chars );

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.