Common php functions-string

Source: Internet
Author: User
Tags rtrim strtok
: Common php functions-string: some common functions that are compiled during php learning. this is a string function .; Helloworldecholtrim ($ str, #).; some common functions sorted out during php learning. this is a string function.

Header ("Content-Type: text/html; charset = UTF-8 ");
// Delete space on both sides or other predefined characters
$ Str = "# hello world @@";
Echo trim ($ str ,'#,@')."
"; // Hello world
Echo ltrim ($ str ,'#')."
"; // Hello world @@
Echo rtrim ($ str ,'@')."
"; // # Hello world
/* Chop () is the alias of rtrim */
// Return the directory part of the path
Echo dirname ("c:/testweb/home. php ")."
"; // C:/testweb
/** String generation and conversion */
// Fill the string in the specified string
$ Str = "hello world ";
Echo str_pad ($ str, 20, '.', STR_PAD_BOTH )."
"; //... Hello world .....
Echo str_pad ($ str, 20, '.', STR_PAD_LEFT )."
"; // ...... Hello world
Echo str_pad ($ str, 20, '.', STR_PAD_RIGHT )."
"; // Hello world .........
// Reuse the specified string
Echo str_repeat (".", 13 )."
";//.............
/* 13 indicates the number of duplicates */
// Splits the string into an array (which contains spaces and one space is 1 character)
$ Str = "my name is Junjun Liu ";
$ Str1 = str_split ($ str, 3 );
Print_r ($ str1 ); // Array ([0] => my [1] => nam [2] => e I [3] => s J [4] => unj [5] => un [6] => Liu)
// Reverse the string
$ Str = "imagecreatetruecolor ";
Echo strrev ($ str )."
"; // Roloceurtetaercegami
// Split the string according to the specified length
$ Str = "An example on a long word is: Supercalifragulisticlasdkjflasdjfalsdkakd ";
Echo wordwrap ($ str, 20 ,"
");
// Randomly disrupt all characters in the string (when a number is disrupted, the delimiter will also be disrupted)
$ Str = "a1, b2, c3 ";
Echo str_shuffle ($ str )."
"; // 1, ca32, B (random)
// Parse the 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 % 20 Adams", $ myArray );
Print_r ($ myArray); // Array ([id] => 23 [name] => John Adams)
// Format the number in a sub-location Group
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
// Case-sensitive conversion
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 associated tags
// Convert string into HTML entity
$ Str = "John & 'Adams '";
Echo htmlentities ($ str, ENT_COMPAT); // John & 'Adams'
// Convert pre-defined characters to html encoding
Htmlspecialchars ($ str );
Echo"
";
// Replace \ n \ r and press enter in the string
Label to implement line feed output
$ String = "This \ r \ nis \ n \ ra \ nstring \ r ";
Echo nl2br ($ string );
/**
* This
* Is
*
* String
*/
// Remove HTML, XML, and PHP labels
$ Str ="AsdasdWoaini ";
Echo strip_tags ($ str); // asdasdwoaini
Echo"
";
// Add characters in the backslash escape string before the specified character
$ Str = "Hello, My name is John Adams .";
Echo addcslashes ($ str, 'M'); // Hello, \ my na \ me is John Ada \ ms
// Delete the addcslashes backslash
Echo stripcslashes ($ str); // Hello, my name is John Adams.
Echo"

";
// Add a backslash before a predefined character
$ Str = "Who's John Ad \ ams? ";
$ Str = addslashes ($ str );
Echo $ str; // Who \'s John Adams?
// Delete the backslash
Echo stripslashes ($ str )."
"; // Who's John Adams?
// Add a backslash before some 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 characters
Echo chr (34); // Return"
// Returns the ASCII value of the first character in the string.
Echo ord (abc); // 97
Echo"
";
/** String comparison */
/*
* 1: The former is large.
*-1: The latter is large.
* 0: Equal
*/
Echo "case-insensitive comparison of two strings:". strcasecmp ("abc", "abd ")."
"; //-1
Echo "compares two strings in case and case:". strcmp ("abd", "Abd ")."
"; // 1
Echo "compares two strings in case sensitivity:". strncmp ("abcd", "abcc", 2 )."
"; // 0/* 2 is to compare the size of the first n strings */
Echo "case-insensitive comparison of two strings:". strncasecmp ("abcd", "abcc", 4 )."
"; // 1
Echo "differentiate size (in natural order) write and compare two strings:". strnatcmp ("abc2", "abc12 ")."
"; //-1
Echo "no size difference (in natural order) write and compare two strings:". strnatcasecmp ("Abc8", "abc12 ")."
"; //-1
/* String cutting and splicing */
// Divide the string into small pieces (space calculation)
$ Str = "hello world ";
Echo chunk_split ($ str, 2, "#"); // 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); (two variables are printed at the same time)
$ Str = "This is an/example string ";
$ Tok = strtok ($ str, "/"); // This is
Echo $ tok;
$ Str = "Thisisan/example string ";
$ Tok = strtok ($ str, "/"); // Thisisan
Echo $ tok;
// Use one string as the flag to separate another string
$ 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 reserved characters into strings
$ Array = array ('lastname', 'Email ', 'phone ');
$ A = implode (",", $ array );
Echo $ a; // lastname, email, phone
// Truncate the string
$ Str = "absadf ";
Echo substr ($ str, 2, 3); // sad
Echo substr ($ str,-4,-1); // sad
/* Search and replace strings */
// String replacement operation, case-sensitive str_replace (replaced font, replaced string, 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 replacement, case insensitive
$ Str = "abcdefg ";
Echo str_ireplace ("ABC", "xyz", $ str); // xyzdefg
// Count the number of times a string appears in another string substr_count (the string to be searched, the string to be searched, the start offset, and the maximum offset)
$ Str1 = "name ";
$ Str2 = "my name isname ";
Echo substr_count ($ str2, $ str1); // 2
// Replace a segment in the string with another string
$ Var = 'abcdefgh:/MNRPQR /';
Echo "Original: $ var \ N "; // Original: ABCDEFGH:/MNRPQR/
/* 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" to the beginning of $ 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/
/* Delete "MNRPQR" from $ var ". */
Echo substr_replace ($ var, '', 10,-1 )."
\ N "; // ABCDEFGH ://
// Returns the similarity between two strings.
$ Str1 = "abcdefgadfsa ";
$ Str2 = "acdrgwsaasdf ";
Echo (similar_text ($ str1, $ str2)/strlen ($ str1) * 100). "% "."
"; // 58.333333333333%
// String search
$ Str = "zhangsan ";
Echo strstr ($ str, "")."
"; // Angsan looks for the position where a appears from the beginning and intercepts it to the end (default: false) alias: strchr ()
Echo strstr ($ str, "a", true )."
"; // Zh start from the front and find the position where a appears and cut it forward
Echo strrchr ($ str, "")."
"; // An starts searching for a from the back and intercepts it to the end.
Echo strpos ($ str, "")."
"; // 2 obtain the position where the first occurrence of a string is
Echo strpos ($ str, "a", 3 )."
"; // 6 get the position where the string appears a from position 3
Echo strrpos ($ str, "")."
"; // 6 obtain the last position of a in the string
// Convert specified characters
$ 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
*
*/
// Return the length of the first substring in the specified character set that contains all the characters in the calculated string.
$ Var = strspn ("42 is the answer to the 128th question.", "1234567890 ");
Echo $ var; // 2 because '42' is the first consecutive character in subject that exists in '123.
// Obtain the length of the starting substring of the unmatched mask
$ 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 */
// Count the number of words contained in the string (the third parameter is ???????????????????????????? ????????????????????????)
$ Str = "My name is John ";
Echo str_word_count ($ str); // 4
Print_r (str_word_count ($ str, 1); // Array ([0] => My [1] => name [2] => is [3] => John)
Print_r (str_word_count ($ str, 2); // Array ([0] => My [3] => name [8] => is [11] => John)
// Count the string length
$ Str = 'AB CD ';
Echo strlen ($ str); // 7
// Count the number of occurrences of all letters in a string (0,255). The number of occurrences of each character is expressed by the corresponding ascii 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 common php functions-strings, including php and strings, and hopes to help those who are interested in PHP tutorials.

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.