Summary of common strings based on PHP (to be continued) _php tutorial

Source: Internet
Author: User
Tags array to string echo date first string strcmp strtok urlencode
1. Splitting and merging
Implode:
Echo Implode (",", Array (' LastName ', ' email ', ' phone '));//Array to string

Explode
Print_r (Explode (",", ' Lastname,email,phone '));//String to array

Split
Print_r (Split ("[/.-]", "2008-9.12"));//or. Or-any symbol is cut into an array

Str_split:
Print_r (Str_split ("Hello Friend", 1));//Cut the string

Preg_split:
Regular segmentation
$ops = Preg_split ("{[+*/-]}", "3+5*9/2");
Print_r ($ops);//return: Array ([0] = 3 [1] = 5 [2] = 9 [3] = 2)

Http_build_query:
Request string after generating url-encoded
$data = Array (' localhost ' = ' AA ',
' User ' = ' BB ',
' Password ' = ' cc ');
echo Http_build_query ($data);//return: LOCALHOST=AA&USER=BB&PASSWORD=CC

Strtok
Cut a string into small pieces
$string = "This Is\tan example\nstring";
Echo strtok ($string, "\n\t");//return: This is
Echo ';
Echo strtok ("\n\t"); When the second time returns: an example
Echo ';
Echo strtok ("\n\t"); When the third time returns: string
2. Find and replace
Many of the strings are r: Take the Last, I: Case insensitive
echo $pos = Strpos (' abcdef abcdaef ', ' a '); The position of the first occurrence of the letter A, case-sensitive
echo $pos = Strrpos (' abcdef abcdeaf ', ' a '); The position of the last occurrence of the letter A, case-sensitive
Stripos: Case insensitive
Strripos: Case insensitive
echo strstr (' user@exa@mple.com ', ' @ ');//return: @exa @mple.com
STRISTR: Case insensitive
echo strchr (' user@exa@mple.com ', ' @ ');//return: @exa @mple.com
STRRCHR: Then return: @mple. com,

Preg_grep:
Returns the array cell that matches the pattern
$food = Preg_grep ("/^p/", Array ("Apple", "Orange", "Pip", "banana"));
Print_r ($food); return: Array ([2] = PIP)

STRTR:
Replaces the found string with the specified array
$arr = Array ("www" = "ftp", "yahoo" = "Baidu");
Echo strtr ("www.yahoo.com", $arr);//return: ftp.baidu.com
Echo strtr ("www.yahoo.com", "Wo", "SX");//return: SSS.YAHXX.CXM Translate string Replace all w with s and replace all o with X

STRSPN:
To find out the length of the original part.
Echo strspn ("ABCDEFG", "1234567890");//return: 0
Find out the length of the initial part without
Echo strcspn ("ABCDEFG", "1234567890");//return: 7


3. Regular
Preg_match:
Returns the number of times that pattern matches. Either 0 times (no match) or 1 times, because Preg_match () stops searching after the first match.
if (Preg_match ("/php/i", "PHP is the Web scripting language of choice."))
echo "exists";
Else
echo "does not exist";

Preg_match_all:
Instead, it searches until the end of the subject.
Preg_match_all ("/\ (?) ( \d{3})? \)? (? (1) [\-\s]) \d{3}-\d{4}/x ",
"Call 555-1212 or 1-800-555-1212", $phones);
Print_r ($phones [0]);//Get all the phone numbers

Ereg_replace:
Replace URL with Hyper-join
Echo ereg_replace ("[[: alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"\\0", ' This is Baidu http://www.baidu.com website. ');
Preg_replace: Filter
$search = Array ("' Si",//Remove JavaScript
"' <[\/\!] *? [^<>]*?> ' Si ',//Remove HTML tags
"' ([\ r \ n]) [\s]+ '",//Remove whitespace characters
"' & (quot| #34); ' I ",//Replace HTML entity
"' & (amp| #38); ' I ",
"' & (lt| #60); ' I ",
"' & (gt| #62); ' I ",
"' & (nbsp| #160); ' I ",
"' & (iexcl| #161); ' I ",
"' & (cent| #162); ' I ",
"' & (pound| #163); ' I ",
"' & (copy| #169); ' I ",
"' (\d+); ' E "); Run as PHP code
$replace = Array ("",
"",
"\\1",
"\"",
"&",
"<",
">",
" ",
Chr (161),
Chr (162),
Chr (163),
Chr (169),
"Chr (\\1)");
echo $text = Preg_replace ($search, $replace, ' Test');

Preg_quote:
Escape the regular expression character, add each to add \, conform to the regular formula.
Echo preg_quote (' $ g3/400 for a ', '/');//return: \$40 for a g3\/400

Sql_regcase:
Produces a regular expression for a match that is not size-sensitive

Echo sql_regcase ("Foo-bar.a"); return: [FF][OO][OO]-[BB][AA][RR]. [Aa]

4.URL encoding processing function
UrlEncode:
echo $str = UrlEncode (' http://www.baidu.com?key= Baidu ');//Encoding
echo UrlDecode ($STR);//decoding

Rawurlencode:
A sequence of percent semicolons (%) followed by a two-digit hexadecimal number is replaced with a literal character
Note: Rawurldecode () does not decode the plus sign (' + ') to a space, while UrlDecode () can.
echo $str = Rawurlencode (' http://www.baidu.com?key= Baidu ');//Encoding
echo Rawurldecode ($STR);

Parse_url:
Parse the URL and return its components
Print_r (Parse_url ("Http://username:password@hostname/path?arg=value#anchor"));

PARSE_STR:
is to parse the URL into a variable
$str = "id=1&name=2";
Parse_str ($STR);
Echo $name;
When there is a second argument, the value is stored in the array
$str = "id=1&name=2";
Parse_str ($str, $array);
Print_r ($array);

5. Time function
Mktime
Convert Date to Timestamp
Echo Time ()-mktime (0,0,0,9,17,2008);//return: The difference between the current and September 17, 2008.
echo Date (' y-m-d h:i:s ');//Current date and time

GetDate
Get Date/Time information
Print_r (GETDATE (Time ()));
6. Compare
Similar_text:
Compare the similarity of two strings
$a = "Hellohhh6";
$b = "hello3hh";
Echo similar_text ($a, $b);//return: 6 Compare how many identical characters are in the corresponding position
echo "
";
Similar_text ($a, $b, $similar);
echo $similar. " %"; Output percent of the same character

Soundex
Compare the pronunciation of two words
$a = "DdHello6";
$b = "Hello3";
Echo soundex ($a). "
";
Echo Soundex ($b). "
";
if (Soundex ($a) ==soundex ($b)) echo "pronounced the same"; else echo ' different ';

STRNATCMP ():
string comparison by natural sorting method
$arr = Array ("A1.jpg", "a2.jpg", "a3.jpg", "a4.jpg");
$max = $arr [0];
for ($i =0; $i {
if (strnatcmp ($arr [$i], $max) >0)
$max = $arr [$i];
}
echo $max;//return: A4.jpg

strcmp
Case-sensitive, string comparison by Byte, return when the first string is greater than the second string: 1, equals return: 0, less than return: 1
echo strcmp (' abc ', ' abc ');
STRCASECMP:
Returns the number of differences between two strings
echo strcasecmp (' WBC ', ' BBC ');//return: 21
strncmp
A string pair that specifies the number of characters, this function is similar, and, unlike that, you can specify the number of characters to be used for a pair of strings. If any one string is shorter than Len, the length of that string is used to compare the
echo strncmp ("Adrdvark", "Aardwolf", 4);//return: 1

7. Sorting
Sort
Rearrange the values of an array by a-Z
$a = Array ("1", "s", "3", "N", "5");//return: 1,3,5,n,s
Sort ($a);//Sort print_r ($a);


8. Other
Str_pad:
The padding string becomes the specified length, Pad_type can be str_pad_right, str_pad_left, or Str_pad_both
Echo Str_pad ("www.yahoo.com", "N", "_", str_pad_both);//The Fill function of the string __www.yahoo.com__
strlen ("AAA");//Find the length of the array return: 3
Strrev ();//The reversal of the string
Strtolower ();//Convert to lowercase
Strtoupper ();//Convert to uppercase
Str_replace () Replaces a string, case-sensitive str_ireplace () is case insensitive
Ucfirst ();//Convert first letter to uppercase
Ucwords ();//Convert the first letter of each word to uppercase
echo Join ("&", Array (' Wo ', ' Men ', ' Shi '));//String connection return: Wo&men&shi with & Connected

Count_chars:
Returns information about the characters used in a string
Print_r (Count_chars ("Hellohhh6", 0));//Returns the number of occurrences of each byte value (0~255) in the string as an array of values. 0 list all of them. 1 columns are more than 0 occurrences. 2 occurrences of a column are equal to 0. 3 Returns a string that consists of the byte values that are used. such as: 6Hehlo. 4 returns a string consisting of unused byte values
Str_replace:
Str_replace ("Yahoo", "Baidu", "www.yahoo.com");
$c = "www.yahoo.com";
$arr = Array ("yahoo", "com");
Echo Str_replace ($arr, "Baidu", $c);//return: Www.baidu.baidu

$c = "www.yahoo.com";
$arr 1 = Array ("www", "yahoo", "com");
$arr 2 = Array ("FTP", "Baidu", "net");
Echo Str_replace ($arr 1, $arr 2, $c);//return: Ftp.baidu.net

substr ($a, 2,2);//extract substring
Echo Substr_count ("This was a test", "is");//The number of occurrences of the statistic substring
Substr_replace ();//Replace substring

$url = "http://localhost/zheng_ze_biao_da/youxiang.php";
Echo substr ($url, Strrpos ($url, "/") +1);//return: Youxiang.php used to return file name

Str_word_count:
$a = "i/love/you/";
echo Str_word_count ($a);//return: 3 number of words in the statistical string
Print_r (Str_word_count ($a, 1));//return: Array ([0] = I [1] = love [2] = = You)
Print_r (Str_word_count ($a, 2));//return: Array ([0] = I [3] = love [9] = = You)
Print_r (Str_word_count ($a, 1, "/")), Return: Array ([0] = i/[1] = love/[2] = = you/) Here is the Ignore "/"

http://www.bkjia.com/PHPjc/327511.html www.bkjia.com true http://www.bkjia.com/PHPjc/327511.html techarticle 1. Split and merge Implode:echo implode (",", Array (' LastName ', ' email ', ' phone '));//array to String Explode:print_r (Explode (",", ' Lastname,email,phone '));//String to array ...

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