PHP Common Functions-string

Source: Internet
Author: User
Tags first string rtrim string find strtok
The process of learning PHP, sorting out some of the commonly used functions, which are string functions.

Header ("Content-type:text/html;charset=utf-8");
Delete both sides (single-sided) spaces or other pre-defined characters
$str = "# #hello world@@";
Echo Trim ($str, ' #,@ '). "
"; Hello World
Echo LTrim ($str, ' # '). "
"; Hello world@@
Echo RTrim ($str, ' @ '). "
"; # #hello World
/* CHOP () is an alias for RTrim () */
Returns the directory portion of the path
Echo dirname ("c:/testweb/home.php"). "
"; C:/testweb
/** string generation and conversion */
Fills a string into the specified string
$str = "Hello World";
Echo Str_pad ($str, ".", Str_pad_both). "
"; ..... hello world .....
Echo Str_pad ($str, ".", Str_pad_left). "
"; .... Hello World
Echo Str_pad ($str, ".", Str_pad_right). "
"; Hello world .....
Reusing the specified string
Echo str_repeat (".", 13). "
"; //.............
/* 13 is the number of repetitions */
Splits a string into an array (containing spaces, a space of 1 characters)
$str = "My name is Junjun Liu";
$str 1 = str_split ($STR, 3);
Print_r ($str 1); Array ([0] = my [1] = nam [2] = = e I [3] = = S J [4] = Unj [5] = = UN [6] = Liu)
Invert string
$str = "Imagecreatetruecolor";
echo Strrev ($STR). "
"; Roloceurtetaercegami
To split a string by a specified length
$STR = "An example on a long word is:supercalifragulisticlasdkjflasdjfalsdkakd";
Echo WordWrap ($str, 20, "
");
Randomly scrambles all the characters in a string (the number is disrupted by the time-interval symbol)
$str = "A1,B2,C3";
echo Str_shuffle ($STR). "
"; 1,ca32,b (Random one)
Parsing a string into a variable
$str = "First=value&arr[]=foo+bar&arr[]=baz";
Parse_str ($STR);
echo $first. "
"; Value
echo $arr [0]. "
"; Foo Bar
echo $arr [1]. "
"; Baz
Print_r ($arr); Array ([0] = foo Bar [1] = baz)
Parse_str ("Id=23&name=john%20adams", $myArray);
Print_r ($myArray); Array ([id] = [name] = John Adams)
Format numbers with thousand-bit groups
Echo Number_format ("1000000"); 1,000,000
Echo Number_format ("1000000", 2); 1,000,000.00
Echo Number_format ("1000000", 2, ",", "."); 1.000.000,00
Uppercase and lowercase conversions
echo strtolower (' NAME '); Name
echo strtoupper (' name '); NAME
echo Ucfirst (' My name is Junjun Liu '); My name is Junjun Liu
echo ucwords (' My name is Junjun Liu '); My Name is Junjun Liu
HTML Associative tags
Convert a string to an HTML entity
$str = "John & ' Adams '";
Echo htmlentities ($str, Ent_compat); John & ' Adams '
Predefined word representable HTML encoding
Htmlspecialchars ($STR);
echo "
";
Replace \ r and enter in the string
tags for line-wrapping output
$string = "this\r\nis\n\ra\nstring\r";
echo nl2br ($string);
/**
* This
* IS
* A
* String
*/
Peel off the html,xml,php label
$str = "ASDASD

Woaini

";
echo Strip_tags ($STR); Asdasdwoaini
echo "
";
Adds a backslash to an escaped character in a string before the specified character
$str = "Hello, My name is John Adams.";
Echo addcslashes ($str, ' m '); Hello, \my Na\me is John Ada\ms
Remove Addcslashes Backslash
echo stripcslashes ($STR); Hello, my name is John Adams.
echo " ";
To add a backslash before specifying a predefined character
$str = "Who ' s John ad\ams?";
$str = Addslashes ($STR);
Echo $str; Who\ ' s John Adams?
Remove backslashes
echo stripslashes ($STR). "
"; Who ' s John Adams?
Add backslashes before certain predefined characters in the string (all characters are escaped)
$str = "Hello World". (Can you hear me?) ";
echo Quotemeta ($STR). "
"; Hello world\.\ (Can you hear me\?\)
ASCII return character
Echo Chr (34); Returns
Returns the ASCII value of the first character in a string
Echo Ord (ABC); 97
echo "
";
/** string comparison */
/*
* 1: Former large
*-1: latter large
* 0: Equal before and after
*/
echo "Case-insensitive comparison of two strings:". STRCASECMP ("abc", "Abd"). "
"; -1
echo "Case comparison two strings:". strcmp ("Abd", "Abd"). "
"; 1
echo "Case comparison between two strings:". strncmp ("ABCD", "ABCC", 2). "
"; 0/* 2 is the comparison of the first n string size */
echo "Case-insensitive comparison of two strings:". STRNCASECMP ("ABCD", "ABCC", 4). "
"; 1
echo "Differentiates size (in natural order) by writing to compare two strings:". STRNATCMP ("ABC2", "ABC12"). "
"; -1
echo "does not differentiate between sizes (in natural order) by writing comparisons between two strings:". STRNATCASECMP ("Abc8", "ABC12"). "
"; -1
/* String cutting and stitching */
Divide a string into small chunks (spaces also count)
$str = "Hello World Hello World";
Echo Chunk_split ($STR, 2, "#"); He#ll#o #wo #rl#d #he #ll#o #wo #rl#d#
Cut string
$first _token = strtok ('/something ', '/');
$second _token = strtok ('/');
Var_dump ($first _token); String (9) "Something"
Var_dump ($second _token); BOOL (FALSE)
Var_dump ($first _token, $second _token);(print two variables simultaneously)
$str = "This is an/example string";
$tok = Strtok ($str, "/"); This was an
Echo $tok;
$str = "Thisisan/example string";
$tok = Strtok ($str, "/"); Thisisan
Echo $tok;
Use one string to split another string for a flag
$data = "Foo:*:1023:1000::/home/foo:/bin/sh";
List ($user, $pass, $uid, $gid, $gecos, $home, $shell) = Explode (":", $data);
Echo $user; Foo
Echo $pass; // *
Concatenate array values with a subscription character into a string
$array = Array (' LastName ', ' email ', ' phone ');
$a = Implode (",", $array);
echo $a; Lastname,email,phone
Intercept string
$str = "ABSADF";
Echo substr ($str, 2, 3); Sad
Echo substr ($str, -4,-1);//sad
/* string Find and Replace */
String substitution operation, case-sensitive str_replace (changed font, changed strings, original string)
$str = "1,2,3:4,5:6";
Echo Str_replace (",", ":", $str). "
"; 1:2:3:4:5:6
echo str_replace (Array (",", ":"), ";", $str). "
"; 1;2;3;4;5;6
echo str_replace (Array (",", ":"), Array (";", "#"), $str). "
"; 1;2;3#4;5#6
String substitution operation, case insensitive
$str = "ABCDEFG";
Echo str_ireplace ("ABC", "XYZ", $STR); Xyzdefg
Counts the number of occurrences of a string in another string Substr_count (search in this string, the string to be searched, the position at which to start the offset, the maximum position of the specified offset)
$str 1 = "name";
$str 2 = "My name Isname name";
Echo Substr_count ($str 2, $str 1); 2
Replace a segment in a string with another string
$var = ' abcdefgh:/mnrpqr/';
echo "Original: $var \ n "; original:abcdefgh:/mnrpqr/
/* These two examples use "Bob" to replace the entire $var. */
Echo substr_replace ($var, ' Bob ', 0). "
\ n "; Bob
Echo substr_replace ($var, ' Bob ', 0, strlen ($var)). "
\ n "; Bob
/* insert "Bob" at the beginning of the $var. */
Echo substr_replace ($var, ' Bob ', 0, 0). "
\ n "; bobabcdefgh:/mnrpqr/
/* The following two examples use "Bob" to replace "MNRPQR" in $var. */
Echo substr_replace ($var, ' Bob ', 10,-1). "
\ n "; abcdefgh:/bob/
Echo substr_replace ($var, ' Bob ',-7,-1). "
\ n "; abcdefgh:/bob/
/* Remove "MNRPQR" from the $var. */
Echo Substr_replace ($var, ", 10,-1). "
\ n "; abcdefgh://
Returns two string similarity degrees
$str 1 = "ABCDEFGADFSA";
$str 2 = "ACDRGWSAASDF";
Echo ((Similar_text ($str 1, $str 2)/strlen ($str 1)) *100). " %"."
"; 58.333333333333%
String Lookup
$str = "Zhangsan";
Echo strstr ($str, "a"). "
"; Angsan from the front to find where a appears and intercept to the last (default false) Alias: STRCHR ()
Echo strstr ($str, "a", true). "
"; Zh from the front to find where a appears and intercept forward
Echo strrchr ($str, "a"). "
"; An A from behind and intercept to the last
Echo Strpos ($str, "a"). "
"; 2 Gets the position of the first occurrence of a string
Echo Strpos ($str, "a", 3). "
"; 6 starting from 3 position gets the location of the string occurrence a
Echo Strrpos ($str, "a"). "
"; 6 Gets the position of the last occurrence of a in a string
Converts a specified character
$trans = Array ("Hello" = "hi", "hi" = "Hello");
echo strtr ("Hi all, I said hello", $trans); Hello all, I said hi
Echo strtr ("Baab", "AB", "01"); 1001
$trans = Array ("AB" = "01");
Echo strtr ("Baab", $trans); Ba01
/*
* STRSTR (): Case sensitive
* STRISTR (): Case insensitive
*/
/*
* Strpos (): Case sensitive
* Stripos () is case insensitive
* Strrpos (): Case sensitive
* Strripos (): Case insensitive
*
*/
Returns the length of the first string in the specified character set that is present in the computed strings.
$var = strspn ("The answer to the 128th question.", "1234567890");
Echo $var; 2 because ' 42 ' is a continuous character that all characters in the first paragraph of the subject are present in ' 1234567890 '.
Gets the length of the starting substring of the mismatched matte
$a = strcspn (' abcd ', ' Apple '); Var_dump ($a); Int (0)
$b = strcspn (' abcd ', ' banana '); Var_dump ($a); Int (0)
$c = strcspn (' Hello ', ' l '); Var_dump ($c); Int (2)
$d = strcspn (' Hello ', ' world '); Var_dump ($d); Int (2)
/* String Statistics */
Statistics string contains the number of words (the third argument????????????????????????? )
$str = "My name is John";
echo Str_word_count ($STR); 4
Print_r (Str_word_count ($STR, 1)); Array ([0] = My [1] = = Name [2] = = [3] = John)
Print_r (Str_word_count ($STR, 2)); Array ([0] = My [3] = = Name [8] = = [one] = John)
Statistical string length
$STR = ' ab CD ';
echo strlen ($STR); 7
Statistic string All letters occurrences (0,255), number of occurrences of each character, and the number of occurrences of the corresponding ASCII code value
$str = "Aaaaasdfasdfwer;dlfgjjpoertuodbldbnlskjl;asfjoiwertowitwo";
echo "
";
Print_r (Count_chars ($STR));
echo "
";
Md5
$str = "hello4521";
echo MD5 ($STR); 5af267d811a324fd640b7ad2199dfe14
echo " ";
/*
function GetMd5 ($STR) {
return MD5 (MD5 ($s). Tri ");
}
*/
Md5_file ()
$str = "Ly.db";
echo Md5_file ($STR); 2f2b2262ed0732d497c90bf62af96240

The above describes the PHP commonly used functions-string, including PHP, string aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.