Chapter 4 data processing-php string processing-Zheng AQI (continued) _ PHP Tutorial

Source: Internet
Author: User
Chapter 4 data processing-php string processing-Zheng AQI (continued ). 1. definition and display definition of a string: "" is used to indicate echo () and print (), but print () has a return value of 1 while echo () does not, however, echo is faster than print (), and print () can be used in replay. 1. definition and display of strings
Definition: marked "",''
Display: echo () and print (), but print () has the return value, 1, while echo () does not, but echo is faster than print (), print () can be used in compound statements.
2. string formatting
Printf (string $ format [, mixed $ args])
The first parameter is the format string, $ args is the value to be replaced, prinf ("% d", $ num );
Description: If you want to print a "%", you must use "%", floating point f, and octal digit "0"
3. common string functions
1. calculate the length of the string
Strlen (string $ string), which indicates that one English character is 1 character, one Chinese character is 2 characters in length, and a space is also a character.
2. change the case sensitivity of the string.
Convert to lower case: strtolower ()
Convert to uppercase: strtoupper ()
Uppercase: ucfirst ()
Uppercase ucwords ()
3. crop the string.
When the beginning and end of a string contain extra spaces, such as spaces and tabs
String trim (string $ str [, string $ charlist])
String rtrim (string $ str [sring $ charlist])
String itrim (string $ str [, string $ charlist])
Table 4.1 default deletion characters for trim, itrim, and rtrim functions

Character

ASCII code

Yi

""

32 (0x20)

Space

"\ T"

9 (0x09)

Tab

"\ N"

10 (0x)

Line Feed

"\ R"

13 (0x0D)

Enter

"\ 0"

0 (0x00)

Null bytes

"\ X0B"

11 (0x0B)

Vertical Tab


4. search strings
String strstr (string $ a, string $ B)
Description: The strstr () function is used to find the position where string pointer $ B appears in string $,
Returns the string starting from $ B to ending with $.
If no value is returned, that is, $ B is not found, FALSE is returned. The strstr () function also has a strchr () function with the same name ().
5. string and ASCII code
4. string comparison
Comparison Functions
Strcmp () // case sensitive
Strcasecmp () // case insensitive
Strncmp () // comparison
Strncasecmp () // case insensitive, comparison part
5. string replacement
Str_replace (search, replace, subject)
Replace the search string in the string subject with the new string replace.
$ Str = "I love you ";
$ Replace = "lucy ";
$ End = str_replace ("you", $ replace, $ str );
Echo $ end; // output "I love lucy"
?>
It is case sensitive and can be used to replace multiple-to-one and multiple-to-many, but cannot be replaced by one-to-multiple.
$ Str = "What Is Your Name ";
$ Array = array ("a", "o", "A", "O", "e ");
Echo str_replace ($ array, "", $ str); // replace multiple-to-one, output "Wht Is Yur Nm"
$ Array1 = array ("a", "B", "c ");
$ Array2 = array ("d", "e", "f ");
Echo str_replace ($ array1, $ array2, "abcdef"); // replace multiple to multiple, and output "defdef"
?>
Substr_replace
Replace part of the string.
6. string and HTML
Omitted
7. other string functions
1. string and array
A. convert a string to an array
The explode () function can use a specified string to split another string and return an array.
$ Str = "use spaces to separate strings ";
Array = explode ("", $ str );
Pint_r ($ array );
Output Array ([0] => use [1] => space [2] => split [3] => string)
?>
B. convert the array to a string
Implode (string $ glue, array $ pieces)
$ Pieces is the array of strings to be connected, and $ glue is the connector used to connect strings. For example:
$ Array = array ("hello", "how", "are", "you ");
$ Str = implode (",", $ array); // use a comma as the connector
Echo $ str; // output "hello, how, are, you"
?>
C. string encryption functions
Md5 (); crypt (), but once encrypted, this function cannot be converted to the original form.
4.3 Handling of instance message book content
A message book contains Email addresses and users' messages. it extracts customers' Email addresses and messages and requires that there be no "." or comma "," before the @ symbol in the Email address.
Use the content in front of the @ symbol in the Email address as the user name, and change the first person "I" in the user's message to "I ".

The code is as follows:




If (isset ($ _ POST ['bt1'])
{
$ Email = $ _ POST ['email ']; // receives the Eamil address
$ Note = $ _ POST ['note']; // receives messages
If (! $ Email |! $ Note) // determine whether to obtain the value
Echo "script" alert (complete the Email address and message! ') Script ";
Else
{
$ Array = explode ("@", $ Email); // separate the Email address.
If (count ($ array )! = 2) // if there are two @ symbols, an error is returned.
Echo "script alert ('email address format error! ') Script ";
Else
{
$ Username = $ array [0]; // get the content before the @ symbol
$ Netname = $ array [1]; // the content after the @ symbol is obtained.
// If username contains "." or "," an error is returned.
If (strstr ($ username, ".") or strstr ($ username ,","))
Echo "script alert ('email address format error! ') Script ";
Else
{
$ Str1 = htmlspecialchars ("<"); // output the symbol "<"
$ Str2 = htmlspecialchars (">"); // The output symbol ">"
// Replace "I" in the message with "I"
$ Newnote = str_replace ("me", "myself", $ note );
Echo "";
Echo "user". $ str1. $ username. $ str2. "Hello! ";
Echo "you are". $ netname. "netizen!
";
Echo"
Your message is:
". $ Newnote ."
";
Echo "";
}
}
}
}
?>

Define: Display echo () and print () by ",'', but print () has the return value, 1, while echo () does not, however, echo is faster than print (), and print () can be used in replay...

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.