Summary of common php string functions-PHP Tutorial

Source: Internet
Author: User
Tags chop strtok
Summary of common php string functions. 1. the formatted output chop is the alias of rtrim (); ltrim () trim () nl2br () converts n to brprint, echo, printf (), sprintf (): echo () it is not a function. print () is a function and has returned values, boolen, 1. format output

Chop is the alias of rtrim;

Ltrim ()

Trim ()

Nl2br () converts \ n


Print, echo, printf (), sprintf ():

Echo () is not a function, print () is a function, there are return values, boolen, false, true;

Printf () format the output

-- Function, which is used to format the text and output it. it directly calls the system call for IO. it is non-buffered. For example:
$ Name = "hunte ";
$ Age = 25;
Printf ("my name is % s, age % d", $ name, $ age );

Sprintf () format the string and assign it to a variable, but it is not output, similar to c

Echo nl2br ("foo ISN'T \ n bar ");
Echo "foo isn' t \ n bar ";
?>

-- Similar to printf, but not printed, the formatted text is returned. Others are the same as printf. For example:
Char SQL [256];
Sprintf (SQL, "select * from table where no = '% s'", bankno );
Its function is to assign the statements in "" to the variable SQL.

Strtolower

Strtoupper

Ucwords

Ucfirst

2. string connection and Segmentation

(1) array explode (string input, string separator, int limit)

Use one string to separate another string

// Example 1
$ Pizza = "piece1 piece2 piece3 piece4 piece5 piece6 ";
$ Pieces = explode ("", $ pizza );
Echo $ pieces [0]; // piece1
Echo $ pieces [1]; // piece2

// Example 2
$ Data = "foo: *: 1023: 1000:/home/foo:/bin/sh ";
List ($ user, $ pass, $ uid, $ gid, $ gecos, $ home, $ shell) = explode (":", $ data );
Echo $ user; // foo
Echo $ pass ;//*

?>


Example 2. limit parameter example

$ Str = 'one | two | three | four ';

// Positive limit
Print_r (explode ('|', $ str, 2 ));

// Negative limit
Print_r (explode ('|', $ str,-1 ));
?>



String strtok (string input, string separator)

$ String = "This is \ tan example \ nstring ";
/* Use tab and newline as tokenizing characters as well */
$ Tok = strtok ($ string, "\ n \ t ");

// Use space, \ n, \ t as the token to separate the string

While ($ tok! = False ){
Echo "Word = $ tok
";
$ Tok = strtok ("\ n \ t ");
}
?>

Result:

Word = This
Word = is
Word =
Word = example
Word = string

(2) string truncation

$ Test = "Your customer service is excellent ";
Echo substr ($ test, 1); ///// our customer service is excellent
Echo"
";
Echo substr ($ test,-9); //// the length starting from the end is 9 excellent
Echo"
";
Echo substr ($ test,); // The length starting from 0 is 4 Your
Echo"
";
Echo substr ($ test, 5,-13);/starts from the fourth to the last 13th characters of customer service
Echo"
";

$ Test = "Your customer service is excellent ";
Echo substr ($ test, 1 );
Echo"
";
Echo substr ($ test,-11 );
Echo"
";
Echo substr ($ test, 0, 6 );
Echo"
";
Echo substr ($ test, 5,-13 );
Echo"
";

Our customer service is excellent
S excellent
Your c
Customer service

(3) join () string link

3. search strings


(1) string strstr (string haystack, string needle) alias: strchr, stristr, and strstr are similar in case insensitive

Strrchr () is opposite. it looks for the last string that appears.

The string that appears for the first time.
$ Email = 'User @ example.com ';
$ Domain = strstr ($ email ,'@');
Echo $ domain; // prints @ example.com
?>
$ Email = 'User @ example.com ';
$ Domain = strstr ($ email, 'E ');
$ Domain2 = strrchr ($ email, 'E'); // The last string that appears
Echo $ domain;
Echo"
";
Echo $ domain2;

Er@example.com
E.com

(2) locate

Int strpos (string str, string needle, [int offset]). the returned value is false.

Returns the position of the needle in str starting from offset.

$ Eg: $ t-'Hello World ';

Echo strpos ($ t, 'O', 5 );

// 7 search for the location of the o variable starting from the o, and the result is 7

Int strrpos ()

5. replace

Str_replace ("% body %", "blank ","





6. Small case


Strpos
Searches for the position of the string that appears for the first time.

Strrpos
Searches for the first occurrence of a character in a string after the first occurrence.

Strpos (stripos case-insensitive)
Strrpos (strripos case-insensitive)






Strstr
Stristr (case-insensitive)

Str_replace
Str_ireplace (case-insensitive)

Explain chop is the alias of rtrim (); ltrim () trim () nl2br () converts \ n to br print, echo, printf (), sprintf (): echo () it is not a function. print () is a function and has returned values, boolen ,...

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.