Php string operation functions-PHP source code

Source: Internet
Author: User
Tags crypt php file upload
Ec (2); php Tutorial: getting started with string operation functions 1. definition and display definition of a string: Use & rdquo; to indicate echo () and print (), but print () has a return value, 1, echo () does not exist, 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 lattice script ec (2); script

Php tutorial getting started with string operation functions
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
ASCII code

32 (0x20)

9 (0x09)

10 (0x)
Line feed

13 (0x0D)

0 (0x00)
NULL bytes

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"
Replace part of the string.
6. String and HTML
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 ";
$ 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 ";
$ 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 ";
$ 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!
Your message is:
". $ Newnote ."
Echo "";

Function prototype: array explode (string separator, string input );

The explode function is widely used. Its main function is to split the specified string with a set separator and return it as an array. It is often used to split file names to determine the file type, cut user Email, and other occasions.

PHP string splitting function explode processing instance

1. Get the file extension

$ FileName = "leapstutorial oulcn.jpg ";
$ Str = explode (".", $ fileName );
Print_r ($ str );

We know that in the PHP file upload function, the most basic way to determine whether the file name to be uploaded is legal is to determine whether the extension is legal. In this case, we need to use the PHP string function explode to split the file name. In the above Code, the explode function uses the. separator to separate file names. The input result is as follows:

Array ([0] => leapsoulcn [1] => jpg)

2. Get user Email domain name information

$ EmailInfo = explode ("@", $ email );


AddSlashes: adds a slash to the string.
Bin2hex: Binary to hexadecimal.
Chop: removes consecutive gaps.
Chr: returns the character of the ordinal value.
Chunk_split: Splits a string into segments.
Convert_cyr_string: Convert the ancient Slavic string to another string.
Crypt: encrypt the string with DES encoding.
Echo: Output string.
Explode: Cut string.
Flush: clears the output buffer.
Get_meta_tags: extracts all metadata marked by meta from the file.
Htmlspecialchars: convert special characters into HTML format.
Htmlentities: converts all characters into HTML strings.
Implode: converts an array into a string.
Join: converts an array into a string.
Ltrim: removes consecutive white spaces.
Md5: Calculate the MD5 of the string.
Nl2br: Convert line breaks
Ord: returns the ordinal value of a character.
Parse_str: parses the query string into a variable.
Print: Output string.
Printf: output the formatted string.
Quoted_printable_decode: convert an qp encoded string to an 8-Bit String.
QuoteMeta: Add a reference symbol.
Rawurldecode: Restores a string from a URL-specific format to a normal string.
Rawurlencode: encode a string into a URL-specific format.
Setlocale: configure the region information.
Similar_text: String similarity calculation.
Soundex: calculates the pronunciation of a string.
Sprintf: format the string.
Strchr: Find the first occurrence character.
Strcmp: String comparison.
Strcspns: the length of different strings.
Strip_tags: removes HTML and PHP tags.
StripSlashes: removes the backslash.
Strlen: gets the string length.
Strrpos: Find the last occurrence of a character in the string.
Strpos: searches for the first occurrence of a character in a string.
Strrchr: gets the string starting from the last occurrence of a character.
Strrev: reverse string.
Strspns: identifies the number of masks that a string falls into another string.
Strstr: returns the string from the beginning to the end of a string.
Strtok: Cut string.
Strtolower: converts all strings to lowercase letters.
Strtoupper: converts all strings to uppercase.
Str_replace: String replacement.
Strtr: converts certain characters.
Substr: obtains some strings.
Trim: removes spaces at the beginning and end of a string.
Ucfirst: uppercase for the first character of the string.
Ucwords: change the first letter of each character in the string to uppercase.

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