PHP common examples of string manipulation functions summary (trim, nl2br, addcslashes, UUDecode, MD5, etc.), nl2braddcslashes_php tutorial

Source: Internet
Author: User
Tags strcmp

PHP common examples of string manipulation functions summary (trim, nl2br, addcslashes, UUDecode, MD5, etc.), nl2braddcslashes


This article summarizes PHP's commonly used string manipulation functions. Share to everyone for your reference, as follows:

/* Common string output function * * the echo () output String * print () outputs one or more strings * die () outputs a message and exits the current script * printf () output formatted string * sprintf () writes the formatted string to a variable **// /ucfirst//Converts the first letter in a string to uppercase $str= "string"; Echo Ucfirst ($STR); echo "
//ucwords ()//capitalize the first letter of each word in the string $ucword= "Hello everyone!"; echo Ucwords ($ucword);
//ltrim () RTrim () trim ()//Remove Space $str= "123 This is a test ..."; Echo LTrim ($str, "0..9"). "
"; Remove the digital echo rtrim ($str, ".") to the left.
"; Echo Trim ($str," 0..9A.) Z. ")."
"; Remove the uppercase letters at both ends of the string, and the numbers.//html related string formatting functions//nl2br ()//convert \ n in string to "
"$STR =" This is \ n Hello world "; Echo nl2br ($str). '
';//htmlspecialchars ()//The HTML tag is displayed 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 method 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 '])//In contrast to the Htmlentities method, converts the encoded HTML character into the form that the browser can compile $a= "I want a bright Future"; $b = htmlentities ($a)."
"Echo $b; echo Html_entity_decode ($b); echo"
";//htmlspecialchars_decode (string $string [, int $flags = Ent_compat | ENT_HTML401]//In contrast to the Htmlspecialchars function, convert the HTML entity to a character $c=htmlspecialchars ($a); Echo $c. "
"; Echo Htmlspecialchars_decode ($c)."
"; Echo"
";//lcfirst (string $str)//The first character of the string is lowercase $str=" Hello world ";//Echo Lcfirst ($STR)."
";//md5_file (string $filename [, bool $raw _output = false])//The file is MD5 encrypted//$string =" password "; $str =md5 ($string); if ($ str== "5f4dcc3b5aa765d61d8327deb882cf99") {echo "The password is right
";} Parse_str (String $str [, Array & $arr])//parse a string into variable and array form $str = "first=value&arr[]=foo+bar&arr[]= Baz ";p arse_str ($str, $input);p Rint_r ($input); echo"
";//string sha1_file (String $filename [, bool $raw _output = false])//computes the hash value of the file, 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])//calculates the similarity of two strings, passing the third argument by reference, Similar_text () Calculates the percentage of similarity to//. $string 1= "Rogerzhalili", $string 2= "Zhangjieroger", if (Similar_text ($string 1, $string 2, $percent)) {echo $string 1. " and ". $string 2." Has the similarity of: ". $percent."
";} echo "
";//string str_shuffle (String $str)//Disturb a string $string=" I want you to solve this problem "; Echo str_shuffle ($string)."
";//array str_split (String $string [, int $split _length = 1])//Divide the string by the specified length $arr=str_split ($string, 3);//str_word_co UNT (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])//finds the last bit of the specified string in the target string in a case-insensitive manner//. Unlike Strrpos (), Strripos () is case insensitive. Offset is used to specify from that location to find $haystack= ' ababcd '; $needle = ' Ab '; echo "the Last". $needle. " Postion is: ". Strripos ($haystack, $needle)."
Echo Strrpos ($haystack, ' ab ');
";//string strstr (String $haystack, mixed $needle [, bool $before _needle = false])//Return haystack string from where the needle first appears The string that starts with the haystack knot//tail. The 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 $needle substring out in $haystack The current number of times, $needle case-sensitive $hay= "La La wa la wa wa lala"; Echo Substr_count ($hay, "la"). "
";//int preg_match_all (String $pattern, String $subject [, Array & $matches [, int $flags = Preg_pattern_order [, in T $offset = 0]])//Regular match, the result of the match is stored 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"
";p Rint_r ($phones); echo"
"; Echo"
";//preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]]//Search SUBJ The part of the ECT that matches the pattern, replaced with replacement. $string = ' April, 2003 '; $pattern = '/(\w+) (\d+), (\d+)/I '; $replacement = ' ${1}1,$3 Echo preg_replace ($pattern, $replacement, $string);
";//array preg_split (String $pattern, string $subject [, int $limit =-1 [, int $flags = 0]])//separates the given string by a regular expression. $str = ' string '; $chars = Preg_split ('//', $STR,-1, Preg_split_no_empty);p Rint_r ($chars);

Read more about PHP string manipulation related content readers can view this site topic: PHP String Usage Summary

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • How the Substr_count () function Gets the number of substrings in PHP
    • PHP uses the STRSTR () function to obtain a method for all characters after a specified string
    • PHP strncmp () function compares the first 2 characters of a two string method
    • STRNATCMP () function "Natural sorting algorithm" in PHP for string comparison usage analysis (compare strcmp function)
    • strcmp () and strcasecmp () function strings in PHP comparison usage analysis
    • Analysis of substr function string interception usage in PHP
    • PHP uses Trim function to remove left and right spaces and special character instances
    • PHP encapsulated string encryption and decryption function
    • The most accurate PHP intercept string length function
    • What to do if you use substr () in PHP to intercept strings in Chinese characters.

http://www.bkjia.com/PHPjc/1089927.html www.bkjia.com true http://www.bkjia.com/PHPjc/1089927.html techarticle php commonly used string operation function Example summary (trim, nl2br, addcslashes, UUDecode, MD5, etc.), nl2braddcslashes This article summarizes PHP commonly used string manipulation functions. To share with you ...

  • 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.