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

Source: Internet
Author: User
Tags rtrim

/*commonly used 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 *< /c1>*///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 a string$ucword= "Hello everyone!";Echo Ucwords($ucword);Echo";//LTrim () RTrim () trim ()//Remove Space$str= "123 This is a test ...";Echo LTrim($str, "0..9"). " <br/> ";//remove the numbers on the leftEcho RTrim($str,".")." <br/> ";Echo Trim($str, "0..9A.. Z. ")." <br/> ";//Remove the uppercase letters at both ends of the string and the numbers.//html related string formatting functions//nl2br ()//convert \ n in string to ' <br/> '$str= "This is \ n Hello World";Echo NL2BR($str).‘ <br/> ';//htmlspecialchars ()//display HTML tags as characters without explanation$str= "<b>hello world</b>";Echo $str." <br/> ";Echo Htmlspecialchars($str);Echo";//addcslashes//Adding backslashes$str=addcslashes("foo[]", "A." Z);Echo $str." <br/> ";Echo addcslashes("zoo[.") ", ' A. Z '). " <br/> ";//Convert_uuencode ()//encode a string using the UUDecode method$string= "Hello World";$str=Convert_uuencode($string);Echo $str." <br/> ";Echo Convert_uudecode($str)." <br/> ";//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 a form that the browser can compile$a= "I want a bright <b>future</b>";$b=htmlentities($a)." <br/> ";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 an HTML entity to a character$c=Htmlspecialchars($a);Echo $c." <br/> ";EchoHtmlspecialchars_decode ($c)." <br/> ";Echo";//Lcfirst (String $str)//lowercase The first character of a string$str= "Hello World";//Echo Lcfirst ($str). " <br/> ";//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 <br/>";}//parse_str (String $str [, Array & $arr])//parse a string into the form of a variable and an array$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])//hash value of the computed fileforeach(Glob("c:/lamp/appache2/htdocs/*.php") as $ent){ if(Is_dir($ent)){ Continue; } Echo $ent." (SHA1: ".Sha1_file($ent).") <br/> ";}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." <br/> ";}Echo";//String str_shuffle (string $str)//disrupts a string$string= "I want solve this problem";Echo Str_shuffle($string)." <br/> ";//array Str_split (string $string [, int $split _length = 1])//split string by specified length$arr=Str_split($string, 3);//str_word_count (String $string [, int $format = 0 [, string $charlist]]//number of words in the statistics stringEcho";//int Strripos (string $haystack, string $needle [, int $offset = 0])//finds the location of the last occurrence of the specified string in the target string in a case-insensitive manner. Unlike Strrpos (), Strripos () is case insensitive. Offset is used to specify where to start the search$haystack= ' ABABCD ';$needle= ' Ab ';Echo"The Last".$needle." Postion is: ".Strripos($haystack,$needle)." <br/> ";Echo Strrpos($haystack, ' AB ');Echo";//string Strstr (String $haystack, mixed $needle [, bool $before _needle = false])//Returns the Haystack string from where the needle first appeared The string that starts at the end of haystack. The function is case-sensitive. If you want to be case-insensitive, use//STRISTR (). $a= "The First Test";$needle= "Fi";Echo strstr($a,$needle)." <br/> ";if($c=strstr($a, "Fio")){ Echo"Find".$c." <br/> ";}Else{ Echo"Not find the string!<br/>";}Echo";//int Substr_count (string $haystack, string $needle [, int $offset = 0 [, int $length]])//Find $needle substring out in $haystack Number of occurrences, $needle case-sensitive$hay= "La la wa la wa wa lala";Echo Substr_count($hay, "La"). " <br> ";//int Preg_match_all (string $pattern, String $subject [, Array & $matches [, int $flags = Preg_pattern_order [, in T $offset = 0]])//regular match to store the matched result in $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"<pre>";Print_r($phones);Echo"</pre>";Echo";//preg_replace (mixed $pattern, mixed $replacement, mixed$subject[, int$limit=-1 [, Int &$count ]] )//Searches for the part of the subject that matches the pattern, replacing it with replacement.$string= ' April 15, 2003 ';$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])//The given string is separated by a regular expression. $str= ' String ';$chars=Preg_split(‘//‘,$str,-1,preg_split_no_empty);Print_r($chars);

RELATED LINKS

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

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.