String array 4th data processing-php string processing-Zheng Achi cont.

Source: Internet
Author: User
Tags rtrim
1. Definition and display of strings
Definition: By "", "" to mark
Display: Echo () and print (), but print () has a return value of 1, and Echo () does not, but Echo is faster than print (), and print () can be used in compound statements.
2. Formatting of strings
printf (String $format [, Mixed$args])
The first parameter is the format string, $args is the value to replace in, Prinf ("%d", $num);
Description, if you want to print a "%", you must use "%", floating-point number F, octal with "0"
3. Commonly used String functions
1. Calculating the length of a string
Strlen (String $string), description, 1 English length 1 characters, 1 Chinese characters are 2 characters long, and spaces are counted as one character.
2. Change the string to case
Convert to lowercase: strtolower ()
Convert to uppercase: Strtoupper ()
Capitalize the first character: Ucfirst ()
Capitalize the first letter of each word ucwords ()
3. String clipping.
When the end of a string has extra white space characters, such as spaces, tabs, etc. can be used
String Trim (String $str [, String $charlist])
String RTrim (String $str [sring $charlist])
String Itrim (String $str [, String $charlist])
Table 4.1 Default Delete characters for trim, Itrim, RTrim functions

Characters

ASCII code

Significance

" "

(0x20)

Space

"\ T"

9 (0x09)

Tabs

"\ n"

Ten (0x)

Line break

"\ r"

(0x0D)

Enter

"The"

0 (0x00)

Empty bytes

"\X0B"

One (0x0B)

Vertical tab


4. Searching for strings
String Strstr (String $a, string $b)
Description: The Strstr () function is used to find the position where the string pointer $b appears in the string $ A.
and returns the string from $b to the end of a $ A string in a $ A string.
If there is no return value, that is, no $b is found, false is returned. The Strstr () function also has a function strchr () with the same name.
5. String and ASCII code
4. Comparison of strings
The comparison function has
strcmp ()//Case Sensitive
STRCASECMP ()//Case insensitive
STRNCMP ()//comparison section
STRNCASECMP ()//Case insensitive, Comparison section
5. Substitution of strings
Str_replace (Search,replace,subject)
Description to 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 for many-to-one, many-to-many replacements, but not for a-to-many substitution.
$str = "What is Your Name";
$array =array ("A", "O", "a", "O", "E");
Echo Str_replace ($array, "", $str); Multiple-to-one replacement, output "WHT is Yur Nm"
$array 1=array ("A", "B", "C");
$array 2=array ("D", "E", "F");
Echo Str_replace ($array 1, $array 2, "abcdef"); Many-to-many replacements, output "Defdef"
?>
Substr_replace
Part of the replacement string.
6. Strings and HTML
Slightly
7. Other String functions
1. Strings and Arrays
A. String conversions to arrays
The explode () function can split another string with the specified string and return an array
$str = "Split string with space";
Array=explode ("", $str);
Pint_r ($array);
Output array ([0] = = use [1] + = space [2] = = Split [3] = = string)
?>
B. Converting an array to a string
Implode (string $glue, array $pieces)
$pieces is the array that holds the string to concatenate, $glue is the connector used to concatenate strings. For example:
$array =array ("Hello", "how", "is", "you");
$str =implode (",", $array); Use commas as connectors
Echo $str; Output "Hello,how,are,you"
?>
C. Cryptographic functions for strings
MD5 (); Crypt (), but once this function is encrypted, it cannot be converted to its original form.
4.3 Example message book content processing
A guest book, the guestbook has an email address and user's message, extract the customer's email address and message, request email address in the @ symbol before a little "." or comma ",".
The email address before the @ symbol as the user's user name, and the user message in the first person "I" modified to "I".

Copy the Code code as follows:




if (Isset ($_post[' bt1 '))
{
$Email =$_post[' Email ']; Receive Eamil Address
$note =$_post[' note ']; Receive Message
if (! $Email | |! $note)//Determine if a value is obtained
echo "";
Else
{
$array =explode ("@", $Email); Split email Address
if (count ($array)!=2)//If there are two @ symbols then an error
echo "";
Else
{
$username = $array [0]; Get the content before the @ symbol
$netname = $array [1]; Get the content after the @ symbol
If the username contains "." or "," then error
if (Strstr ($username, ".") or Strstr ($username, ","))
echo "";
Else
{
$str 1= Htmlspecialchars ("<"); Output symbol "<"
$str 2= htmlspecialchars (">"); Output symbol ">"
Replace "I" with "I" in the message
$newnote =str_replace ("I", "I", $note);
echo "";
echo "User". $str 1. $username. $str 2. "Hello!";
echo "You are". $netname. Netizens!
";
echo "
Your message is:
". $newnote."
";
echo "";
}
}
}
}
?>

The above describes the string array in the 4th chapter of data processing-php string processing-Zheng Achi, including the contents of the string array, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.