Php string function learning

Source: Internet
Author: User
String functions are indispensable during development. there are too many functions and it is a little impossible to back them up. However, if you use a function, you should know which function can be implemented.

String functions are indispensable during development. there are too many functions and it is a little impossible to back them up. However, when using a function, you should also know which function can be implemented, at least I need to be impressed. let's start to learn about the php string functions. Remember to be impressed!

/**
String function learning
*/
// Trim (string [, deleted letter]) deletes the white space between the left and right sides of the string
// Ltrim () // delete the white space on the left of the string
// Rtrim () // delete the white space on the right of the string
$ _ Str = "helloh world ";
Echo "++ ";
Echo trim ($ _ str, 'hdelo ');
Echo "------";
Exit;

// Str_replace (Replaced content, replaced content, string) string replacement
// 3. str_replace (arr, arr, str );
$ _ Str = "today, is, Thursday, and, I, am, happy ";
$ Arr1 = array ('today', 'is ');
$ Arr2 = array ('tommorow', 'was ');
Echo str_replace ($ arr1, $ arr2, $ _ str );
Exit;
// 2. str_replace (arr, str, str );
$ _ Str = "today, is, Thursday, and, I, am, happy ";
$ Arr = array ('today', 'is ');
Echo str_replace ($ arr, 'tommorow', $ _ str );
Exit;
// 1. str_replace (str, str, str)
$ _ Str = "today, is, Thursday, and, I, am, happy ";
Echo str_replace ('today', 'tommorow', $ _ str );
Exit;

// Substr (target string, start position, length) intercepts the string [the most important function of the string]
// Start from 0 on the left
// Start from-1 on the right
$ _ Str = "today, is, Thursday, and, I, am, happy ";
// Echo substr ($ _ str, 9, 8); // Thursday
// Echo substr ($ _ str,-9, 8); //, am, happ
// Echo substr ($ _ str,-9,-8 );//,
// Echo substr ($ _ str, 9,-8); // Thursday, and, I,
Echo substr ($ _ str, 9,-24 );
Exit;

// Implode ([separator,] array) combines array elements into a string
$ _ Arr = array ('A', 'B', 'C ');
Echo implode ('-', $ _ arr );
Exit;
// Explode (separator, string) // splits the string into small units of information and returns an array.
$ _ Str = "today, is, Thursday, and, I, am, happy ";
Print_r (explode (',', $ _ str ));
Exit;

/**
String definition
String definition: single quotation marks, double quotation marks, and delimiters
Single quotes :\'\\
Double quotation marks: \ $ \ n \ r \ t "\\\[\{
Delimiters: < Delimiter content
Eof;
*/
$ Abc = <

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.