Php Getting Started Tutorial (19) php string processing

Source: Internet
Author: User
Tags string back
Php Getting Started Tutorial (19) php string processing

  1. $ Str = "Hello world. It's a beautiful day .";
  2. Print_r (explode ("", $ str ));
  3. ?>

2. example of string connection:

  1. // Define a string
  2. $ Str1 = "Hello World! ";
  3. $ Str2 = "PHP! ";
  4. // Separate the two strings with spaces
  5. $ Str3 = $ str1. "". $ str2;
  6. // Output the connected string
  7. Echo $ str3;

3. string truncation substr (), mb_substr example:

  1. $ Str = "login.com ";
  2. Echo substr ($ str, 2); // gin.com
  3. Echo substr ($ str, 2, 3); // gin
  4. Echo substr ($ str,-2); // Obtain the negative value of om from the end.
  5. ?>

However, when you intercept Chinese strings, garbled characters are easily displayed, because one Chinese character is two bytes while one English letter is one byte.

Solution: 2, mb_substr (). use the same method as substr, but enable extension = php_mbstring.dll extension in php. ini.

Example:

  1. Echo mb_substr ("int Hello", "UTF-8"); // nt you
  2. ?>

3. string search strpos () definition and usage the strpos () function returns the position where the string first appears in another string. If this string is not found, false is returned.

Syntax strpos (string, find, start)

The parameter description string must specify the string to be searched. find must specify the characters to be searched. Start is optional. Specifies the location where the search starts

Example:

  1. $ String = '#11 #22 #33 #44 #55 #66 #77 ';
  2. $ Find = '#55 ';
  3. If (strpos ($ string, $ find) === false ){
  4. Echo 'not found ';
  5. } Else {// bbs.it-home.org
  6. Echo 'found ';
  7. }
  8. Echo strpos ($ string, $ find );
  9. ?>

4. string replacement str_replace example:

  1. $ Replace_string = 'You are very bad, really bad ';
  2. $ Replace_string_ B = 'bad ';
  3. $ Replace_string _ = str_replace ($ replace_string_ B, '*', $ replace_string, $ I );
  4. Echo $ replace_string _;
  5. ?>

Replace substr_replace with the specified position

  1. Echo substr_replace ("Hello world", "php12", 6); // Hello php12
  2. ?>

5. the string-to-case function converts a string to lower-case strtolower (): This function converts all characters of the input string parameter to lower-case, and places the string back in a small form. example:

  1. $ Str = "I love YOU ";
  2. $ Str = strtolower ($ str );
  3. Echo $ str;
  4. ?>

Output result: I love YOU

Convert character to uppercase strtoupper (): This function is opposite to the strtolower function. it converts all characters of the input character parameter to uppercase and returns the string in uppercase. the usage is the same as that of strtolowe.

Convert the first character of the string to usfilst (): This function is used to change the first character of the string to uppercase. This function returns the first character of the string. The usage is the same as that of strtolowe.

Convert the first character of each word in a string to uppercase ucwords (): This function converts the first character of each word in the input string to uppercase. For example, "hello world". after this function is processed, "Hello Word" is returned ". The usage is the same as that of strtolowe.

>>> View more 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.