string Handling in PHP

Source: Internet
Author: User
Tags rtrim

printf type specifier

Specifiers

Significance

B

parameter is an integer and appears as a binary number

C

argument is an integer and appears as a character corresponding to the value

D

parameter is an integer and appears as a decimal number

E or F

parameter is a double-precision number and is displayed as a floating-point

G

parameter is a precision double and is displayed as a floating-point number

O

parameter is an integer and appears as an octal (base 8) number

S

parameter is a string and is displayed as a string

U

parameter is an unsigned integer and is displayed as a decimal number

X

parameter is an integer and appears as a hexadecimal (base 16) number, in lowercase letters

X

parameter is an integer and appears as a hexadecimal (base 16) number, in uppercase

Format a Date:

printf ('%02d/%02d%04d ', $month, $day, $year);

Output: 02/15/2002

A floating-point number is converted to only two decimal places:

printf ('%.2f ', 27.452);

Output: 27.45

--------------------------------------------------------

Organizing strings

1. Remove whitespace characters

You can use the trim (), LTrim (), RTrim () functions to remove whitespace from the string masthead or end:

$trimmed = Trim (string[,charlist]);

$trimmed = LTrim (String[,charlist]);

$trimmed = RTrim (String[,charlist]);

Trim () Returns a copy of a string that has the opening and trailing spaces removed

LTrim () (l means left) to do the same work, but only remove the white space character

RTrim () (R = right) removes only the white space character to the left of the string.

2. Change the case

PHP has some functions for changing the case of Strings: Strtolower () and Strtoupper () manipulate the entire string, Ucfirst () only the first letter of the string, and the first letter of each word in the Ucwords () operation String. Each function takes an manipulated string as a parameter and returns a copy of the string that has been appropriately changed. For example:

$string 1 = "FRED Flintstone";

$string 2 = "Barney Rubble";

Print (Strtolower ($string 1));

Print (Strtoupper ($string 1));

Print (Ucfirst ($string 2));

Print (Ucwords ($string 2));

In turn, the result is:

Fred Flintstone

FRED Flintstone

Barney Rubble

Barney Rubble

-----------------------------------------

Encoding and escaping

Because PHP interacts most often with HTML pages, Web addresses (URLs), and databases. So there are some functions to help you deal with those data types. Although both the Html,web page address and the database command are strings, each of them requires different characters to be escaped in different ways, for example, a space in the Web address must be written as% 20, and a direct amount less than (<) in the HTML document must be written in &lt; , PHP has "many built-in functions" to convert these encodings.

------------------------------------------------------

(i) HTML

Special characters in HTML are represented by entities such as &amp; and &lt;. There are two functions for converting special characters in strings to their entities, one for deleting HTML tags and the other for extracting meta tags. • Entity references to all special characters the htmlentities () function converts all characters (except whitespace) with the HTML entity equivalents. These characters include the less than sign (<), the greater than sign (>), the & symbol, and the emphasis character.

string Handling in PHP

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.