Eighth chapter string Processing _php Tutorial

Source: Internet
Author: User
Tags chop strcmp strtok
Note: The article is from Eon to PHP video tutorial, this article is limited to exchange use, not for commercial purposes, otherwise the consequences.

Learning Essentials:
1. String formatting
2. Manipulating substrings
3. String comparisons
4. Find Replacement strings
5. Working with Chinese characters

In daily programming, processing, tuning, and finally controlling strings is an important part of the process, and is generally considered
This is the basis for all programming languages. Unlike other languages, PHP is not so cumbersome to use data types to handle words
Character string. In this way, the string processing in PHP is easier.

A String formatting

The first step in organizing a string is to clean up extra spaces in the string. Although this operation is not required, if
This is useful for storing a string in a file or database, or comparing it to other strings.
The chop () function removes extra whitespace after the string, including the new line.
The LTrim () function removes extra whitespace at the beginning of the string.
The RTrim () function removes extra whitespace after the string, including the new line, which is the alias of Chop ().
The trim () function removes extra whitespace on both sides of the string.

 
  < span="">< span="">('        PHP       '< span="">?>

PHP has a series of functions that can be used to reformat strings, and these functions work in different ways.
The same.
The NL2BR () function takes a string as an input parameter, using the
Tag instead of a newline character in a string.

 
  < span="">< span="">("This was a teacher!\nthis is a student!"  < span=""> ?>

To convert special characters to HTML equivalents, you can use the Htmlentities () and Htmlspecialchars functions.
If you want to remove the HTML from the string, you can use the Strip_tags () function

 
  < span="">< span="">(' I am Wu Qi! (' I am Wu Qi!  < span="">  < span="">  < span="">  < span="">  (' I am Wu Qi!  < span="">  < span="">  < span="">  < span="">     ?> < span=""> 

Some characters are definitely valid for strings, but when inserting data into a database, you might
Cause some problems, because the database interprets these characters as controls. These problematic characters are the quotation marks (a single citation
and double-quoted), backslash (\), and null characters.
PHP provides two functions that are designed to escape a string. Before you write any strings to the database, you should
Use Addslashes () to reformat them,
After Addslashes () is called, all quotes are added with slashes, and the stripslashes () function removes the slashes.

 
  < span="">< span="">(' This is \a ' teacher! '< span="">?>

You can reformat the case of letters in a string.
The Strtoupper () function converts a string to uppercase
Strtolower () function converts a string to lowercase
The Ucfirst () function converts the first letter to uppercase
The Ucwords () function converts the first letter of each word to uppercase

 
  < span="">< span="">(' yc60.com@gmail.com '< span="">?>

Fill string function: Str_pad () fills the string with a specified number of characters.

 
  < span="">< span="">(' Salad ', 10). ' Is good. '  < span=""> ?>

Two Manipulating substrings

In general, we want to look at the various parts of a string. For example, view a word in a sentence, or use a domain name or a power
The sub-mailing address is divided into component parts. PHP provides several string functions to implement this function.
Using functions explode (), implode (), and join ()
In order to implement this function, the first function we will use is explode ().
Use the implode () or join () function to achieve the opposite effect of function explode (), the effect of which is a
of the cause.

 
  < span="">= ' yc60.com@gmail.com< span="">< span="">'< span="">(' @ ')  < span=""> ,   < span=""> ?>

Use the Strtok () function
The Strtok () function extracts only a few fragments (called tokens) from a string at a time. For removing one from a string at a time
The Strtok () function works better than the explode () function for word processing.

 
  < span="">=< span="">< span="">"I,will.be#back"< span="">(, ",.< span="">#"  < span="">< span="">(< span="">< span="">< span=""> "< span="">
< span="">"< span="">(",. # "?>< span="">< span="">

Use the substr () function
The function substr () allows us to access a substring of the given start and end of a string. This function does not apply
In our case, however, it is useful when you need to get a portion of a fixed-format string.

 
  < span="">< span="">("ABCdef", 1, 3< span="">?>

Decomposition string: Str_split () returns an array where each array element is one of the characters in the string argument
String.

 
  < span="">(< span="">' This is a teacher! '  < span=""> ?>

Inverse string: strrev () can reverse a string backwards.

 
  < span="">< span="">(' This is a teacher! '  < span=""> ?>

Three string comparison

So far, we have used the "= =" number to compare whether two strings are equal. Using PHP, you can make a
more complex comparisons. These comparisons fall into two categories: partial matching and other cases.
Ordering of Strings: strcmp (), strcasecmp (), and strnatcmp ()
The function requires two parameter strings for comparison. If the two strings are equal, the function returns 0 if
Returns a positive number in dictionary order after str1 and str2 (greater than STR2), and returns a negative if str1 is less than str2
Number. This function is case-sensitive.
The function strcasecmp () is the same as strcmp () except for case insensitive.
The function strnatcmp () and its corresponding case-insensitive strnatcasecmp () function are added in PHP4.
The two functions compare strings by "natural sort", so-called natural sorting is sorted by the order in which people are accustomed.

 
  < span="">< span="">(' A ', ' B '< span="">?>

Use the STRSPN () function to return the length of a string that contains the first part of a character in another string. Also
is to ask for the same part between two strings.

 
  < span="">< span="">(' Gmail ', ' yc60.com@gmail.com '< span="">?>

To test the length of a string using the strlen () function
You can use the function strlen () to check the length of a string. If a string is passed to it, the function returns
The length of the string. For example, strlen ("Hello") will return 5.

 
  < span="">< span="">(' This is a teacher! '  < span=""> ?>

Determines how often a string appears: Substr_count () returns the number of occurrences of a string in another string.

 
  < span="">< span="">(' yc60.com@gmail.com ', ' C '< span="">?>

Four Find replacement string

In general, we need to check whether a longer string contains a specific substring. This partial match
is usually more useful than testing the exact equivalent of a string.
Find strings in Strings: Strstr (), STRCHR (), STRRCHR (), and Stristr ()
The function strstr () is the most common, it can be used in a longer string to find a matching string or word
Character. Note that the function strchr () and strstr () are exactly the same.

 
  < span="">< span="">(' yc60.com@gmail.com ', ' @ '< span="">?>

There are two variants of the function strstr (). The first variant is STRISTR (), which is almost the same as STRSTR (), where the difference is not the area
The split character size. For our only form application, this function is useful because the user can enter
"Delivery", "delivery" and "delivery".
The second variant is STRRCHR (), which is almost identical to Strstr (), except that it is an alias of Strstr ().
Find the location of the string: Strpos (), Strrpos ().
The operation of function Strpos () and Strrpos () is similar to STRSTR (), but it does not return a substring and returns a sub-word
The character string needle the position in the haystack. More interestingly, the current PHP manual recommends using Strpos ()
The function replaces the STRSTR () function to see where a substring appears in a string, because the former runs at a speed

Faster.

 
  < span="">< span="">(' yc60.com@gmail.com ', ' C '< span="">?>

Replacement strings: Str_replace (), Str_ireplace (), Substr_replace ()

 
  < span="">< span="">(' @ ', ' # ', ' yc60.com@gmail.com< span="">< span="">< span="">' (' yc60.com@gmail.com ', ' # # # '< span="">, 0,5?>

Five. Working with Chinese characters

For the above string functions, some can be used in Chinese, but some are not in Chinese. So, PHP provides
A special function to solve such a problem.
Chinese characters can be gbk,utf8,gb2312
Mb_strlen () the corresponding function for strlen () to find the length of the string
MB_STRSTR () the corresponding function for STRSTR () to find a string to the end of the character
Mb_strpos () corresponds to the function of Strpos () to find the first occurrence of the character
MB_SUBSTR () The corresponding function takes out the specified string for substr ()
Mb_substr_count () corresponding function returns the number of occurrences of a string for SUBSTR_STR ()

Finally sweep through the Help manual

http://www.bkjia.com/PHPjc/748764.html www.bkjia.com true http://www.bkjia.com/PHPjc/748764.html techarticle Note: The article is from Eon to PHP video tutorial, this article is limited to exchange use, not for commercial purposes, otherwise the consequences. Key points of Study: 1. String formatting 2. Manipulating substring 3 ...

  • 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.