PHP Introductory Tutorial String function Learning Notes (1/3)

Source: Internet
Author: User
Tags chop explode php language php redirect rtrim strlen trim

The PHP language is a powerful embedded HTML scripting language, and its ease of use allows many programmers to choose between them. PHP to determine the inclusion of a string, you can use PHP's built-in function Strstr,strpos,stristr direct judgment. You can also write a judgment function by using the function of explode.

Explore how to implement PHP to determine whether the string is IP
An in-depth discussion on the specific methods of PHP automatically acquiring keywords
A summary of the classification of PHP string functions
Sharing PHP functions require () specific tips for using
Introduction to the specific implementation method of PHP redirect Web page
The following is a detailed description of how the PHP decision string is contained:
1. Strstr: Returns a string from the start of the judged character to the end, and if there is no return value, does not contain

The code is as follows:

The code is as follows Copy Code
< PHP
/* As an example in the manual * *
$email = ' user@example.com ';
$domain = Strstr ($email, ' @ ');
Echo $domain;
Prints @example. com
?>

2. STRISTR: It is exactly the same as strstr. The only difference is that STRISTR is case-insensitive.

3. Strpos: Returns a Boolean value. False and true are not to be said. Use "= = =" to judge. Strpos is faster than two functions in execution speed, and Strpos has a parameter that specifies the location of the decision, but the default is null. The disadvantage is that the Chinese support is not good.

The PHP judgment string contains the following code:

The code is as follows Copy Code
$STR = ' abc ';
$needle = ' a ';
$pos = Strpos ($str, $needle);

4. Use Explode to judge

The PHP judgment string contains the following code:

The code is as follows Copy Code
function Checkstr ($STR) {
$needle = "a";//decide whether to include a character
$tmparray = Explode ($needle, $str);
if (count ($tmparray) >1) {
return true;
} else{
return false;
}
}

The above is the specific code example that the PHP judgment string contains.

Remove the white space character at the end of the string or any character-trim-returns the removed string

The code is as follows Copy Code
/* Format: Trim (string, ' special character to be removed can be multiple ') * *
$str =trim (' admin ');
Echo $str; Admin has no spaces on either side

echo $str =trim (' admin ******** ', ' * '); Admin
echo strlen ($STR); 5 through the length of the word have has not been removed


Remove the white-space character or any character on the right side of the string-RTrim-Returns the removed string

The code is as follows Copy Code
/* Format: RTrim (String, ' special character to be removed can be multiple ') * *
$str =rtrim (' admin ');
Echo $str; No spaces on the right side of ' admin '

echo $str =rtrim (' admin ******** ', ' * '); Admin
echo strlen ($STR); 10 through the length of the word have has not been removed


Remove the white space character or any character to the right of the string-Chop-Chop is RTrim alias

The code is as follows Copy Code
/* Format: Chop (string, ' special character to be removed can be multiple ') * *
$str =chop (' admin ');
Echo $str; No spaces on the right side of ' admin '

echo $str =chop (' admin ******** ', ' * '); Admin
echo strlen ($STR); 10 through the length of the word have has not been removed


Remove whitespace or any character from the left of the string-LTrim-Returns the removed string

The code is as follows Copy Code

/* Format: LTrim (String, ' special character to be removed can be multiple ') * *
$str =rtrim (' admin ');
Echo $str; No spaces on the right side of ' admin '

echo $str =ltrim (' admin ******** ', ' * '); ' Admin ******** '
echo strlen ($STR); 14 through the length of the word have has not been removed


Home 1 2 3 last

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.