PHP string functions

Source: Internet
Author: User

PHP string functions

Reading directory

Determine the string length
Comparison string
Reverse split connection
Html and String Conversion
Fill and remove strings
Count characters and words
Search for replacement interceptions
Case sensitivity

Php has 98 built-in string functions (in addition to regular expression-based functions, regular expressions are not covered here), which can handle every aspect of the string, this article briefly summarizes common string functions, including the following eight parts: 1. 2.2.2. 3.3.3.3.3.3.3. 4.4.4.4.html and String Conversion, 5. fill and remove strings, 6. count the number of characters and words, 7. search for replacement interceptions, 8. case sensitivity.
Back to directory
Determine the string length
Strlen and mb_strlen functions. The latter must enable the mbstring extension.

<? Php
Header ('content-type: text/html; charset = UTF-8 ');
$ Str = 'abcdef ';
Echo strlen ($ str); // 6
Echo "<br/> ";
$ Str = 'AB Cd ';
Echo mb_strlen ($ str); // 7
Echo "<br/> ";
// Strlen is the length of the calculated string "Byte"
// Mb_strlen: calculates the number of "characters" of a string based on encoding.

$ Str = 'people's Republic of China ';
Echo "the length of a byte is". strlen ($ str); // In UTF-8 encoding, one Chinese Character occupies three bytes and one Chinese Character occupies two bytes in gbk
Echo "<br/> ";
Echo "the character length is". mb_strlen ($ str, 'utf-8 ');
?>

Back to directory
Comparison string
Strcmp functions, strcasecmp functions, strspns functions, strcspns Functions

<? Php
$ Pwd = "userpwd ";
$ Pwd2 = "Userpwd ";
// Case sensitive
If (strcmp ($ pwd, $ pwd2 )! = 0 ){
Echo "password do not match ";
} Else {
Echo "password match ";
}

$ Email1 = "www.baidu.com ";
$ Email2 = "WWW. BAIDU. COM ";
// Case insensitive
If (! Strcasecmp ($ email1, $ email2 )){
Echo "OK", '<br> ';
}
// Obtain the same part of the two strings
$ Password = "1233345 ";
If (strspn ($ password, "1234567890") = strlen ($ password )){
Echo "the password connot consist solely of numbers ";
}
//
$ Password = "a12345 ";
If (strcspn ($ password, "1234567890") = 0 ){
Echo "the password connot consist solely of numbers ";
}

?>

Back to directory
Reverse split connection
Str_split function, split function, explode function, and implode Function

<? Php
Header ('content-type: text/html; charset = UTF-8 ');
$ Str = "Hello Friend ";

$ Arr1 = str_split ($ str );
Print_r ($ arr1 );

$ Arr2 = str_split ($ str, 3 );
Print_r ($ arr2 );

$ Str = 'abc, China, US, Japan ';
// Explode: Splits a string into an Array Based on the specified delimiter.
$ Arr = explode (',', $ str );
Print_r ($ arr );
// Implode: concatenates an array into a string based on the specified connector.
$ Arr = explode (',', $ str );
Echo implode ('~ ', $ Arr),' <br/> ';
// You can transmit only one array as a parameter without specifying a connector,
// In this way, the array unit is directly spliced.
Echo implode ($ arr );

?>

Back to directory
Html and String Conversion
Htmlspecialchars function, strip_tags function, get_html_translation_table function, addcslashes function, and htmlentities Function

<? Php

$ Str = "hello", world ";
Echo $ str, '<br/> ';
Echo $ str = addslashes ($ str), '<br/> ';
Echo stripslashes ($ str), '<br/> ';
$ Str = '<AB> ';
Echo $ str, '<br/> ';
Echo htmlspecialchars ($ str );
Echo "</br> ";
$ Str = "Email <a href = 'admin @ qq.com '> example@qq.com </a> ";
Echo strip_tags ($ str );

?>

Back to directory
Fill and remove strings
Trim function, ltrim function, rtrim function, str_pad function, chunk_split Function

<? Php
$ Str = '000000 ';
Echo chunk_split ($ str, 3 ,',');
Echo "<br> ";
$ Text = "\ t \ tThese are a few words :)...";
Echo trim ($ text );
Echo "<br> ";
Echo ltrim ($ text, '\ t'),' <br> ';
Echo rtrim ($ text, '\ R'),' <br> ';
Echo str_pad ('apple', 6). "is good .";
?>

Back to directory
Count characters and words
Count_chars function and str_word_count

<? Php
$ Data = "Two Ts and one F .";

Foreach (count_chars ($ data, 1) as $ I => $ val ){
Echo "There were $ val instance (s) of \" ", chr ($ I)," \ "in the string. \ n ";
}

Echo "$ Str = "Hello fri3nd, you're looking good today! ";

Print_r (str_word_count ($ str, 1 ));

?>

Back to directory
Search for replacement interceptions
Strpos function, str_replace function, substr_replace function, substr function, strstr Function

<? Php
$ Substr = "index.html ";
$ Log = <logfile
192.168.1.11:/www/htdocs/index.html: [2016/08/10: 21: 58: 27]
192.168.1.11:/www/htdocs/index.html: [2016/08/18: 01: 51: 37]
192.168.1.11:/www/htdocs/index.html: [2016/08/20: 11: 48: 27]
Logfile;

$ Pos = strpos ($ log, $ substr );
$ Pos2 = strpos ($ log, "\ n", $ pos );
$ Pos = $ pos + strlen ($ substr) + 1;
$ Timestamp = substr ($ log, $ pos, $ pos2-$ pos );
Echo "The file $ substr was first accessed on: $ timestamp ";
Echo "<br> ";
$ Author = "lester@example.com ";
$ Author = str_replace ("@", "at", $ author );
Echo "connect the author of this article at $ author ";
Echo "<br> ";
Echo ltrim (strstr ($ author ,"@"),"@");

?>

Back to directory
Case sensitivity
Strtolower function, strtoupper function, ucfirst function, ucwords Function

<? Php
$ Url = "http://WWWW.BAIDU.COM ";
Echo strtolower ($ url), '<br> ';
$ Str = "hello world ";
Echo strtoupper ($ str), '<br> ';
$ Str = "php is the most popular language ";
Echo ucfirst ($ str), '<br> ';
Echo ucwords ($ str );
?>

This article Reprinted from: http://www.cnblogs.com/weblm/p/5791016.html

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.