string-related handler functions

Source: Internet
Author: User
Tags rtrim

1. The difference between single and multiple quotes

(1) Single quotation marks: the string enclosed in single quotation marks can no longer contain single quotation marks, that is, single quotation marks cannot be nested. If it is necessary to represent a single quotation mark in single quotation marks, you need to escape with a backslash (\). In addition, the variables that appear in the single-quote string are not substituted by the value of the variable.

echo ' this \ n is \ r a \ simple string \ \ '; Only the last backslash escape output a backslash, and the other escapes are invalid and will be output as-is

  

$STR = 100;

Echo ' A simple $str string '; The variable is $stri as it is, and does not parse the variable in single quotes

Using single quotes is more efficient when defining simple strings.

(2) Double quotation marks: the transfer character "\" is consistent with other words to represent a special character, usually some nonprinting character (e.g. \ n), and if the view escapes other characters, the backslash itself is also displayed. In addition, the variables that appear in the double-quote string are substituted by the value of the variable, that is, you can parse the variable in double quotes.

$beer = ' Heineken ';

echo "$beer ' s taste is great"; The variable $beer can be parsed because (') is not valid in the variable name.

echo "He drank some $beers"; The variable $beers cannot be parsed because "s" is valid in the variable name and does not $beers the variable

echo "He drank some ${beer}s"; With {}, you can separate the variables and parse the

echo "He drank some {$beer}s"; You can parse a variable, another use of {}

2. Commonly used string output functions

2.1 Echo ()

This function is used to output one or more strings, and Echo () is not actually a function (it is a language structure), so it is not necessary to use parentheses. If you pass one or more parameters to echo (), you cannot use parentheses.

$str = "What's LAMP?";

Echo $str; Direct output string variable cannot be parentheses

Echo ' This ', ' string ', ' is ', ' made ', ' width multiple parameters<br> '; Multiple parameters separated by commas can be output

2.2 printf ()

The function has the same function as Echo (), it has a return value, and if it returns 1 successfully, the failure returns 0. But it doesn't perform as efficiently as the Echo () function.

2.3 Die ()

The function is an alias for the exit () function. If the argument is a string, the function outputs it before exiting. If the argument is an integer, the value is used as the exit state. The value of the exit status is between 0 and 254. Exit status 255 is reserved by PHP and will not be used. Status 0 is used to successfully terminate the program.

$url = "Http://www.brophp.net";

fopen ($url, "R") or Die ("Unable to connect to $url"); Output a message and exit the program if open fails

2.4 printf ()

Format: printf (FORMAT,ARG1,ARG2....ARGN)

The function is used to output a formatted string, the first parameter is a required option, is the specified string and how the variable is formatted. You can also have more than one optional parameter, which is the parameter that specifies the corresponding% symbol for the formatted string that is inserted into the first parameter. The conversion format used in the first argument is to start with the percent sign ("%") to the end of the conversion character (for example:%d, which represents the signed decimal number, and the syntax with the C language). The following multiple parameters are inserted into the percent sign (%) symbol in the main string. This function is executed step-by. Insert a arg1 in the first% symbol, insert arg2 at the second% symbol, and so on. If the% symbol is more than the arg parameter, you must use a placeholder. Placeholders are inserted after the% symbol, which consists of numbers and "\$".

$str = "LAMP";

$num = 798;

printf ("%s book.page number%u <br>", $str, $num); $str the string to the output of the output in the first argument, as a string of%s, the integer $num output by%u

2.5 sprintf ()

The use of this function is similar to Prinf (), but it is not an output string, but rather a formatted string written to a variable in the form of a return value. This allows the formatted string to be used when needed.

$num = 12345;

$text = sprintf ("%0.2f", $num);

Echo $text; 12345.00

3. Commonly used string formatting functions

3.1 Removing whitespace and string fill functions

3.1.1 LTrim (), RTrim (), Trim ()

Format: String LTrim (String str[,string charlist])

String RTrim (String str[,string charlist])

String Trim (String str[,string charlist])

These three functions are used to remove white-space characters or other predefined characters from the left, right, and ends of a string, respectively. The processed results are returned as a new string and are not modified on the original string. Where the first parameter, str, is the string to be processed, the required option. The second parameter, charlist, is a filter string that specifies the special symbol you want to remove, which is optional. If you do not specify a filter string, the following characters are removed by default.

* "": space

* "+": NULL

* "\ T": Tab

* "\ n": Line break

* "\ r": Enter

You can also use the ".." The symbol specifies a range that needs to be removed, such as "0..9" or "A." Z "means to remove the numbers and lowercase letters in ASCII values.

$STR = "lamp"; Declares a string with three spaces on the left and two spaces on the right with a total length of 9 characters

echo strlen ($STR); 9

Echo strlen (LTrim ($STR)); 6

Echo strlen (RTrim ($STR)); 7

Echo strlen (Trim ($STR)); 4

$STR = "123" is a test ... ";

Echo LTrim ($str, "0..9"); Output: This is a test ...

Echo RTrim ($str, "."); Output: 123 This is a test

Echo Trim ($str, "0..9 A.  Z. "); Output: He is a test

3.1.2 Str_pad ()

Prototype: string Str_pad (string input, int pad_length[,string pad_string[,int Pad_type])

This function is used to fill the string as required. The function has 4 parameters, the first parameter is a required option, and the string to be processed is named. The second parameter is also a required option, given the length of the string after processing, and if the value is less than the length of the original string, no action is made. The third parameter specifies the string to fill with, which is an optional parameter and, if not specified, uses a space to fill by default. The last parameter specifies the direction of the fill, which has three optional values: Str_pad_both, Str_pad_left, and Str_pad_right, respectively, representing the padding at both ends of the string, left and right. is also an optional parameter, if not specified, the default value is Str_pad_right.

$str = "LAMP";

Echo Str_pad ($STR, 10); Output: Lamp length is 10, by default use space to fill "lamp" on the right

Echo Str_pad ($str, ten, "-=", str_pad_left); Output:-=-=-=lamp

Echo Str_pad ($str, Ten, "_", Str_pad_both); Output: ___lamp___

Echo Str_pad ($STR, 6, "___"); Output: lamp__

3.2 String-Case conversion

Example: $lamp = "lamp is composed of Linux, apace, MySQL and PHP";

3.2.1 Strtoupper ()

This function is used to convert the given string all to uppercase, with only one argument

echo Strtoupper ($lamp); Output: LAMP is composed of LINUX, APACHE, MYSQL and PHP

3.2.2 Strtolower ()

This function is used to convert the given string all to lowercase letters, with only one argument

echo Strtolower ($lamp); Output: Lamp is composed of Linux, Apache, MySQL and PHP

3.2.3 Ucfirst ()

The function is used to convert the first letter of the given string to uppercase, the remaining letters unchanged, and only one parameter

echo Ucfirst ($lamp); Output: Lamp is composed of Linux, Apache, MySQL and PHP

3.2.4 Ucwords ()

This function is used to convert the first letter of a space-delimited word in a given string to uppercase, with only one argument

echo Ucwords ($lamp); Output: Lamp is composed of Linux, Apache, MySQL and PHP

3.3 and HTML tag-related string formatting

3.3.1 NL2BR ()

The function inserts the HTML line break "<br/>" before each new line "\ n" in the string.

Echo nl2br ("One line.\nanother line.");

Output: one line

Another line

  

  

string-related handler functions

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.