Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of string manipulation functions commonly used in PHP

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the PHP commonly used string operation function example analysis, the article introduces in great detail, has a certain reference value, interested friends must read it!

The details are as follows:

/ * commonly used string output function * * echo () output string * print () outputs one or more strings * die () outputs a message and exits the current script * printf () outputs a formatted string * sprintf () writes the formatted string into a variable * * / ucfirst / / converts the first letter of the 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); echo ""; / / ltrim () rtrim () trim () / / remove spaces $str= "123 This is a test."; echo ltrim ($str, "0.9"). "; / / remove the number echo rtrim ($str,". ")."; echo trim ($str, "0..9A..Z."). " / / remove uppercase letters from both ends of the string, numbers and. / / HTML-related string formatting function / / nl2br () / / convert\ n in the string to "" $str= "this is\ n hello world"; echo nl2br ($str).''; / / htmlspecialchars () / / display the html tag in character form without interpreting $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 the 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']]) / / contrary to the htmlentities method, converts the encoded html character into a form that the browser can compile: $a = "I want a bright future"; $b = htmlentities ($a). "; echo $bx echo html_entity_decode ($b); echo" / / htmlspecialchars_decode (string $string [, int $flags = ENT_COMPAT | ENT_HTML401]) / / convert the HTML entity to the character $c=htmlspecialchars ($a); echo $c. "; echo htmlspecialchars_decode ($c)."; echo "; / / lcfirst (string $str) / / lowercase the first character of the string $str=" Hello World "; / / echo lcfirst ($str)." / / md5_file (string $filename [, bool $raw_output = false]) / / A pair of files are encrypted with md5 / / $string= "password"; $str=md5 ($string); if ($str== "5f4dcc3b5aa765d61d8327deb882cf99") {echo "The password is right";} / / parse_str (string $str [, array & $arr]) / / parses a string 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 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 between two strings, passes the third parameter by reference, and similar_text () calculates the percentage of similarity. $string1= "rogerzhalili"; $string2= "zhangjieroger"; if (similar_text ($string1,$string2,$percent)) {echo $string1. " And ". $string2." Has the similarity of: ". $percent.";} echo ""; / / string str_shuffle (string $str) / / scrambles 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 by 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]) / / looks for the last occurrence of the specified string in the target string in a case-insensitive manner. Unlike strrpos (), strripos () is not case-sensitive. / / offset is used to specify where to find $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 location where needle first appeared to the haystack knot / tail. This function is case sensitive. If you want to be case insensitive, use / / stristr (). $a = "the First test"; $needle= "Fi"; echo strstr ($a, Fio). ""; 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 substring appears in $haystack, $needle is case-sensitive and $hay= "lala 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, store the matching 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 ""; print_r ($phones); echo ""; echo ""; / / preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]]) / / search for the part of subject that matches pattern and replace it with replacement. $string = 'April 15,2003potential potential patterns =' / (\ w +) (\ d +), (\ d +) / iPreventive organs ='${1} 1pm 3pm echo preg_replace ($pattern,$replacement,$string); echo " / / array preg_split (string $pattern, string $subject [, int $limit =-1 [, int $flags = 0]]) / / A given string is separated by a regular expression. $str = 'string';$chars = preg_split (' / /', $str,-1, PREG_SPLIT_NO_EMPTY); print_r ($chars). These are all the contents of the article "sample Analysis of Common string manipulation functions in PHP". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 273

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report