PHP strings (String)

Source: Internet
Author: User
Tags intl parse error
A string of strings consists of a series of characters, each of which is equal to one byte. This means that PHP can only support a 256 character set and therefore does not support Unicode. See detailed string types.

The note:string can reach 2GB maximum.

Grammar

A string can be expressed in 4 different ways:

Single quotation marks

Double quotes

HEREDOC syntax structure

NOWDOC syntax structure (since PHP 5.3.0)

Single quotation marks

The simplest way to define a string is to enclose it in single quotation marks (characters ').

To express a single quotation mark itself, you need to escape it by adding a backslash (\) in front of it. To express a backslash itself, use two backslashes (\ \). Any other backslash is treated as a backslash itself: that is, if you want to use a different escape sequence such as \ r or \ n, it does not represent any special meaning, it is simply the two characters themselves.

Note: Unlike double quotes and HEREDOC syntax structures, the escape sequences of variables and special characters in single-quote strings will not be replaced.

<?phpecho ' This was a simple string '; Can input multiple lines echo ' You can also has embedded newlines instrings this "as it isokay to do '; Output: Arnold once said: "I'll be Back" Echo ' Arnold once said: "I\ ' ll is back" '; Output: Deleted C:\*.*?echo ' you deleted c:\\*.*? '; Output: Deleted C:\*.*?echo ' you deleted c:\*.*? '; Output: This would not expand: \ n A newlineecho ' this would not be expand: \ n a newline '; Output: Variables do not $expand $eitherecho ' Variables does not $expand $either '; >

Double quotes

If the string is enclosed in double quotation marks ("), PHP will parse some special characters:

Sequence

Meaning

\ n

NewLine (LF or 0x0A (10) in the ASCII character set)

\ r

Enter (CR or 0x0D (13) in the ASCII character set)

\ t

Horizontal tab (HT or 0x09 (9) in the ASCII character set)

\v

Vertical tab (VT or 0x0B (11) in the ASCII character set) (since PHP 5.2.5)

\e

Escape (ESC or 0x1B (27) in the ASCII character set) (since PHP 5.4.0)

\f

Page break (FF or 0x0C (12) in the ASCII character set) (since PHP 5.2.5)

\\

Reverse Slash

\$

Dollar mark

\"

Double quotes

\[0-7]{1,3}

A character that conforms to the regular expression sequence is a eight-binary expression

\x[0-9a-fa-f]{1,2}

A character that conforms to the regular expression sequence is a 16-binary expression

As with single-quote strings, escaping any other character causes backslashes to be displayed. Before PHP 5.1.1, the backslash in the \{$var} was not displayed.

The most important feature of a string defined in double quotation marks is that the variable is parsed, as described in variable parsing.

Heredoc structure

The third way to express strings is to use the HEREDOC syntactic structure:<<<. After the operator, provide an identifier, and then wrap the line. This is followed by the string itself, and finally with the identifier defined earlier as the end flag.

The identifier referenced at the end must be in the first column of the row, and the name of the identifier follows the rules of PHP like any other label: it can only contain letters, numbers, and underscores, and must begin with a letter and an underscore.

Warning

Note that the end identifier line must not contain any other characters, except for the possibility of a semicolon (;). This means that the identifier cannot be indented, and the semicolon cannot have any blank or tab characters before or after it. More importantly, the end identifier must be preceded by a newline that is recognized by the local operating system, such as \ n in UNIX and Mac OS X systems, and an end delimiter (possibly followed by a semicolon) that must be followed by a newline.

If the rule is not followed and the end identity is not "clean", PHP will assume that it is not the end identifier and continue to look for it. If a correct end identifier is not found before the end of the file, PHP will generate a parse error on the last line.

The HEREDOCS structure cannot be used to initialize the properties of a class. Since PHP 5.3, this restriction is only valid for heredoc containing variables.

Example #1 Illegal Example

<?phpclass Foo {public   $bar = <<<eotbar   EOT;}? >

The HEREDOC structure is like a double-quote string that does not use double quotes, which means that the single quotes in the HEREDOC structure are not escaped, but the escape sequences listed above are also available. The variables will be replaced, but be careful when they contain complex variables in the HEREDOC structure.

Example Example of string #2 Heredoc structure

<?php$str = <<<eodexample of stringspanning multiple linesusing heredoc syntax. EOD; /* More complex example containing variables */class foo{   var $foo;   var $bar;    function foo ()   {       $this->foo = ' foo ';       $this->bar = Array (' Bar1 ', ' Bar2 ', ' Bar3 ');}   } $foo = new Foo (); $name = ' MyName '; echo <<<eotmy name is "$name". I am Printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should print a capital ' a ': \x41eot;? >

The above routines will output:

My name is "MyName". I am printing some Foo.
Now, I am printing some Bar2.
This should print a capital ' a ': a

You can also use the HEREDOC structure in function parameters to pass data:

Example of Example #3 Heredoc structure in Parameters

<?phpvar_dump (Array (<<<eodfoobar! EOD));? >

After PHP 5.3.0, you can also use the HEREDOC structure to initialize the properties and constants of static variables and classes:

Example #4 Use the HEREDOC structure to initialize static values

<?php//static variable function foo () {   static $bar = <<<labelnothing in here ... LABEL;} Class constants, Attributes Class foo{   const BAR = <<<foobarconstant Examplefoobar;    Public $baz = <<<foobarproperty Examplefoobar;}? >

Nowdoc structure

Just as the heredoc structure resembles a double-quote string, the Nowdoc structure is similar to a single-quote string. The NOWDOC structure is much like the heredoc structure, but no parsing is performed in the Nowdoc. This structure is ideal for embedding PHP code or other large pieces of text without escaping special characters. And the <! of SGML [cdata[]]> structure is used to declare a large segment of the text like no parsing, nowdoc structure has the same characteristics.

A NOWDOC structure is also marked with the same <<< as the HEREDOCS structure, but the following identifiers are enclosed in single quotes, i.e. <<< ' EOT '. All the rules of the HEREDOC structure also apply to the NOWDOC structure, especially the rules for ending identifiers.

Example #6 Nowdoc Structure String Example

<?php$str = <<< ' EOD ' Example of stringspanning multiple linesusing nowdoc syntax. EOD; /* More complex example with variables */class foo{public   $foo;   public $bar;    function foo ()   {       $this->foo = ' foo ';       $this->bar = Array (' Bar1 ', ' Bar2 ', ' Bar3 ');}   } $foo = new Foo (); $name = ' MyName '; echo <<< ' EOT ' My name is ' $name '. I am Printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should not print a capital ' a ': \x41eot;? >

The above routines will output:

My name is "$name". I am Printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital ' a ': \x41

Note:

Unlike the HEREDOC structure, the NOWDOC structure can be used in any static data environment, and the most typical example is a property or constant used to initialize a class:

Example of Example #7 static data

<?phpclass Foo {public   $bar = <<< ' EOT ' Bareot;}? >

Note:

The NOWDOC structure was added in PHP 5.3.0.

Variable resolution

When a string is defined by a double-quote or HEREDOC structure, the variable is parsed.

There are two grammatical rules: a simple rule, a complex rule. Simple grammar rules are the most common and convenient, and can be used to embed a variable in a string with a minimum of code, an array value, or an object property.

The significant markup for complex rule syntax is an expression surrounded by curly braces.

Simple syntax

When the PHP parser encounters a dollar sign ($), it will, like many other parsers, combine as many identities as possible to form a valid variable name. You can use curly braces to clarify the line of variable names.

<?php$juice = "Apple"; echo "He drank some $juice juice." php_eol;//Invalid. "S" is a valid character for a variable name, and the variable is $juice. echo "He drank some juice made of $juices.";? >

The above routines will output:

He drank some apple juice.
He drank some juice made of.

Similarly, an array index or an object property can also be parsed. The array index is to use square brackets (]) to represent the edge of the end of the index, and the object properties are the same as the variable rules above.

Example #8 Simple Syntax example

<?php$juices = Array ("Apple", "orange", "koolaid1" = "purple"); echo "He drank some $juices [0] juice." Php_eol;echo "He drank some $juices [1] juice." Php_eol;echo "He drank some juice made of $juice [0]s.] Php_eol; Won ' t Workecho "He drank some $juices [koolaid1] Juice." Php_eol; class People {public   $john = "John Smith";   Public $jane = "Jane Smith";   Public $robert = "Robert Paulsen";   Public $smith = "Smith";} $people = new People (); echo "$people->john drank some $juices [0] juice." Php_eol;echo "$people->john then said hello to $people->jane." Php_eol;echo "$people->john ' s wife greeted $people->robert." Php_eol;echo "$people->robert greeted the $people->smiths."; Won ' t work?>

The above routines will output:

He drank some apple juice.
He drank some orange juice.
He drank some juice made of S.
He drank some purple juice.
John Smith drank some apple juice.
John Smith then said hello to Jane Smith.
John Smith ' s wife greeted Robert Paulsen.
Robert Paulsen greeted the other.

If you want to express a more complex structure, use complex syntax.

Complex (curly brace) syntax

Complex syntax is not named because of its complex syntax, but because it can use complex expressions.

Any scalar variable with a string expression can use this syntax for array cells or object properties. Simply write the expression as if it were outside the string, and enclose it in curly braces {and}. Because {cannot be escaped, only $ is recognized next to {. You can use {\$ to express {$. The following example provides a better explanation:

<?php//Displays all error error_reporting (E_all); $great = ' fantastic '; Invalid, output: This was {Fantastic}echo "this is {$great}"; Valid, output: This is Fantasticecho ' this is {$great} '; echo "This Is ${great}"; Effective echo "This square is {$square->width}00 centimeters broad."; Valid, only through curly brace syntax to correctly parse the quoted key name Echo "This works: {$arr [' key ']}"; Effective echo "This works: {$arr [4][3]}"; This is the wrong expression, because the format of the $foo [bar] is not the same as the string. In other words, it works only if PHP can find the constant Foo, which results in A//e_notice (undefined constant) level error. echo "This is wrong: {$arr [foo][3]}"; Valid, when using multiple arrays in a string, be sure to enclose it in parentheses echo "This works: {$arr [' foo '][3]}"; Effective echo "This works:". $arr [' foo '][3]; echo "This works too: {$obj->values[3]->name}"; echo "This is the value of the Var named $name: {${$name}}"; echo "This is the value of the Var named by the return value of GetName (): {${getname ()}}"; echo "This is the value of the Var named by the return value of \ $object->getname (): {${$object->getname ()}}"; Invalid, output: This is the return value ofGetName (): {getName ()}echo "This is the return value of GetName (): {GetName ()}";? >

You can also use this syntax in a string to invoke the properties of a class by using a variable.

<?phpclass Foo {   var $bar = ' I am bar ';} $foo = new foo (); $bar = ' bar '; $baz = Array (' foo ', ' Bar ', ' baz ', ' Quux '); e Cho "{$foo-$bar}\n"; echo "{$foo-$baz [1]}\n]; >

The above routines will output:

I am Bar.

I am Bar.

Note:

Functions, methods, static class variables, and class constants can only be used in {$} after PHP 5. However, its value can be accessed as a variable name only if the string is defined in the namespace. Single-use curly braces ({}) cannot handle the value of a return value from a function or method or a class constant and a class static variable.

<?php//Displays all error error_reporting (E_all); Class Beers {   Const Softdrink = ' Rootbeer ';   public static $ale = ' IPA ';} $rootbeer = ' A & W '; $ipa = ' Alexander keith\ ' s '; Valid, output: I ' d like A & Wecho "I ' d like an {${beers::softdrink}}\n"; Also valid, output: I ' d like a Alexander Keith ' Secho "I ' d like a {${beers:: $ale}}\n";? >

Accessing and modifying characters in a string

The characters in string can be accessed and modified using a 0-based subscript that contains the corresponding number in brackets similar to the array structure, such as $STR [42]. You can think of string as a character array. Functions substr () and Substr_replace () can be used in cases where more than one character is manipulated.

Note:string can also be accessed using curly braces, such as $str {42}.

Warning

Writing with an subscript that exceeds the length of the string will lengthen the string and fill it with spaces. Bidding clubs are converted to integers under non-integer types. Illegal subscript types produce a e_notice level error. Writing a string with a negative subscript results in a e_notice level error, and an empty string is returned when the string is read with a negative subscript. Only the first character of the assignment string is used when writing. Assigning a value with an empty string assigns a value that is a NULL character.

Warning

The PHP string is internally an array of bytes. Therefore, it is not safe to use curly braces to access or modify strings to multibyte character sets. This type of operation should only be done for single-byte encodings such as iso-8859-1 strings.

Example #9 Some examples of strings

<?php//gets the first character of the string $str = ' This is a test. '; $first = $str [0]; Gets the third character of a string $third = $str [2]; Gets the last character of the string $str = ' This is still a test. '; $last = $str [Strlen ($STR)-1]; Modify the last character of the string $str = ' Look at the sea '; $str [Strlen ($STR)-1] = ' e ';? > From PHP 5.4 The string subscript must be an integer or a string that can be converted to an integer, otherwise a warning is issued. Before for example "Foo" the next bidding clubs silently converted to 0. Example #10 PHP 5.3 and PHP 5.4 differ <?php$str = ' abc '; Var_dump ($str [' 1 ']); Var_dump (Isset ($str [' 1 '])); Var_dump ($str [' 1.0 ']); Var_dump (Isset ($str [' 1.0 '])); Var_dump ($str [' X ']); Var_dump (Isset ($str [' X '])); Var_dump ($str [' 1x ']); Var_dump (Isset ($str [' 1x ']);? >

The above routines are output in PHP 5.3:

String (1) "B"
BOOL (TRUE)
String (1) "B"
BOOL (TRUE)
String (1) "a"
BOOL (TRUE)
String (1) "B"
BOOL (TRUE)

The above routines are output in PHP 5.4:

String (1) "B"
BOOL (TRUE)

Warning:illegal string Offset ' 1.0 ' in/tmp/t.php on line 7
String (1) "B"
BOOL (FALSE)

Warning:illegal string offset ' x ' in/tmp/t.php on line 9
String (1) "a"
BOOL (FALSE)
String (1) "B"
BOOL (FALSE)

Note:

variables that use [] or {} To access any other type (which does not include an array or an object with the corresponding interface) will only silently return NULL.

Note:

PHP 5.5 adds support for accessing characters directly in string prototypes using [] or {}.

Useful functions and operators

Strings can be used '. ' (dot) operator, note that the ' + ' (plus) operator does not have this function. For more information, refer to string operators.

There are many useful functions for the operation of String.

You can refer to string functions for most functions, and advanced Find and replace functions can refer to regular expression functions or Perl-compatible regular expression functions.

There are also URL string functions, as well as functions for encrypting/decrypting strings (MCrypt and Mhash).

Finally, you can refer to the character type function.

Convert to String

A value can be converted to a string by adding (string) before it or by using the Strval () function. In an expression that requires a string, it is automatically converted to string. For example, this conversion occurs when you use the function echo or print, or when a variable is compared to a string. Type and type conversions can better explain the following, or refer to the function Settype ().

A Boolean value of Boolean TRUE is converted to string "1". The Boolean FALSE is converted to "" (an empty string). This conversion can be done between a Boolean and a string.

An integer or floating-point number float is converted to a literal string of numbers (including the exponential portion of float). Floating-point numbers (4.1E+6) that use exponential notation can also be converted.

Note:

Decimal dot characters are defined in the area of the script (category Lc_numeric). See SetLocale ().

Array arrays are always converted to the string "array", so echo and print cannot display the contents of the array. To display a cell, you can use echo to $arr the structure of [' Foo ']. To display the entire array contents, see below.

In PHP 4 object objects are always converted to the string "Object", if you want to print out the value of the object for debugging reasons, continue reading the following. In order to get the name of the class of the object, you can use the Get_class () function. From PHP 5, you can use the __tostring method when appropriate.

The resource resource is always transformed into a string of structures such as "Resource ID #1", where 1 is the unique value that PHP assigns to the resource at run time. Do not rely on this structure and there may be changes. To get a resource type, you can use the function Get_resource_type ().

Null is always transformed into an empty string.

As mentioned above, converting array,object or resource directly into a string will not get any useful information other than its type. These types of content can be listed using Functions Print_r () and Var_dump ().

Most PHP values can be converted to strings for permanent preservation, which is called serialization and can be implemented using the function serialize (). If the PHP engine is set to support wddx,php values can also be serialized as well-formed XML text.

Convert string to Numeric

When a string is taken as a numeric value, the result and type are as follows:

If the string does not contain '. ', ' e ' or ' e ' and its numeric value is within the range of the integral type (as defined by Php_int_max), the string will be evaluated as an integer. All other cases are evaluated as float.

The starting part of the string determines its value. If the string starts with a valid numeric value, the value is used. Otherwise its value is 0 (0). The legal value is represented by an optional sign, followed by one or more digits (which may have decimal points), followed by an optional exponential portion. The exponential part is composed of one or more digits followed by ' e ' or ' e '.

<?php$foo = 1 + "10.5";                $foo is float (11.5) $foo = 1 + " -1.3e3";              $foo is float ( -1299) $foo = 1 + "bob-1.3e3";           $foo is integer (1) $foo = 1 + "BOB3";                $foo is integer (1) $foo = 1 + "Small Pigs";       $foo is integer $foo = 4 + "10.2 Little piggies"; $foo is float (14.2) $foo = "10.0 pigs" + 1;          $foo is float (one) $foo = "10.0 pigs" + 1.0;        $foo is float (one)?>

For more information, refer to Strtod (3) in the Unix manual.

The examples in this section can be displayed by copying/pasting into the following code:

<?php

echo "\ $foo = = $foo; Type is ". GetType ($foo). "<br/>\n";

?>

Don't imagine that in C, by converting a character to an integer to get its code. Use the function ord () and Chr () to convert between ASCII code and characters.

String type Details

The string in PHP is implemented by a byte array plus an integer indicating the buffer length. There is no information on how to convert bytes into characters, as determined by the programmer. There is no limit to what the string is composed of and, in particular, the byte with a value of 0 ("NUL bytes") can be anywhere in the string (although there are several functions that are referred to in this manual as non-"binary security", it may be possible to omit the data after the NUL byte).

This attribute of the string type explains why there is no separate "byte" type in PHP-it has been replaced with a string. A function that returns a non-literal value-for example, any data read from a network socket-will still return a string.

Since PHP does not specifically specify the encoding of the string, how exactly is the string encoded? For example, is the string "á" exactly equal to "\xe1" (iso-8859-1), "\xc3\xa1" (utf-8,c form), "\x61\xcc\x81" (utf-8,d form) or any other possible expression? The answer is that the string is encoded in the same way that the script file is encoded. So if the encoding of a script is iso-8859-1, then the string in it will be encoded as iso-8859-1, and so on. However, this does not apply when Zend multibyte is activated; The script can be encoded in any way (explicitly specified or automatically detected) and then converted to an internal encoding, and then the string is encoded in this way. Note that the encoding of the script has some constraints (if Zend multibyte is activated)-This means that the encoding should be a compatible superset of ASCII, such as UTF-8 or iso-8859-1. It is important to note, however, that the encoding of the dependent state where the same byte value can be used for both initial and non-initials is converted to a state, which can cause problems.

Of course, to be useful, the function that operates the text must assume how the string is encoded. Unfortunately, PHP has many variants of this function:

Some functions assume that a string is encoded in a single byte, but do not need to interpret the byte as a specific character. Examples include substr (), Strpos (), strlen (), and strcmp (). Another way to understand these functions is that they work in memory buffers, that is, by byte and byte subscripts.

Some functions are passed into the encoding of the string, and it may be assumed that there is no such information by default. For example, most functions in the htmlentities () and mbstring extensions.

Other functions use the current region (see setlocale ()), but are byte-wise. Examples include strcasecmp (), Strtoupper (), and Ucfirst (). This means that these functions can only be used for single-byte encoding, and the encoding must match the region. For example, Strtoupper ("á") returns "Á" when the locale is set correctly and á is a single-byte encoding. If you encode with UTF-8, the correct result is not returned, and the result may return a corrupted value based on the current region.

Finally, some functions assume that a string is a specific encoding, usually a UTF-8. This is true for most of the functions in the Intl extension and PCRE (only when you have used the U modifier in the previous example). Although this is due to its special purpose, Utf8_decode () assumes that UTF-8 encoding and Utf8_encode () assumes iso-8859-1 encoding.

Finally, writing a program that correctly uses Unicode relies on being careful to avoid functions that might corrupt the data. To use functions from the Intl and mbstring extensions. But using a function that can handle UNICODE encoding is just the beginning. Regardless of the language provided in the function, the most basic is to understand the Unicode specification. For example, if a program assumes only uppercase and lowercase, that's a big mistake.

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