PHP Common String Operation function Example summary trim, NL2BR, addcslashes, UUDecode, MD5, etc.

Source: Internet
Author: User
Tags php programming rtrim
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 "[Object Object]";//ucwords ()//capitalize the first letter of each word in the string ucword= "Hello everyone!"; echo Ucwords ($ucword); echo "[Object Object]";//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= ""; Echo $str. ""; echo Htmlspecialchars ($STR); echo "[Object Object]";//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 to the form that the browser can compile $a= "I want a bright"; $b = Htmlentit IES ($a). ""; echo $b; Echo Html_entity_decode ($b); echo "[Object Object]";//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 "[Object Object]";//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" [Object Object] ";//string sha1_file (String $filename [, bool $raw _ Output = false])//Calculate hash value of file foreach (Glob ("c:/lamp/appache2/htdocs/*.php") as $ent) { if (Is_dir ($ent)) {continue;} echo $ent. " (SHA1: ". Sha1_file ($ent).") "; echo "[Object Object]";//int similar_text (String $first, String $second [, float & $percent])//calculates the similarity of two strings, by means of a reference Pass the third parameter, 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 "[Object Object]";//string str_shuffle (String $str)//Disturb a string $string= "I want you to solve this problem"; Echo str_sh Uffle ($string). "";  /array str_split (String $string [, int $split _length = 1])//Split the string by 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 "[Object Object]";//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 '); echo "[Object Object]";//string strstr (String $haystack, mixed $needle [, BOOL $before _nee Dle = false])//Returns the Haystack string starting from the first occurrence of the needle to the haystack knot//tail string. 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 "[Object Object]";//int substr_count (String $haystack, string $needle [, int $offset = 0 [, int $length]])//Find $n The number of occurrences of the eedle substring in $haystack, $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 [, int $ 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" "; echo" [Object Object];//preg_replace (mi Xed $pattern, mixed $replacement, mixed $subject [, int $liMIT =-1 [, int & $count]]//Search for the part of subject that matches the pattern and replace it with replacement. $string = ' April, 2003 '; $pattern = '/(\w+) ( \d+), (\d+)/I '; $replacement = ' ${1}1,$3 '; Echo preg_replace ($pattern, $replacement, $string); echo "[Object object];// Array Preg_split (String $pattern, string $subject [, int $limit = 1 [, int $flags = 0])//The given string is separated by a regular expression. $str = ' s Tring '; $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.

The above describes the PHP commonly used string operation function Example summary trim, NL2BR, addcslashes, UUDecode, MD5, etc., including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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