What is a Querystring?
Strings are mainly used for programming. See the text for concept descriptions, function explanations, and usage details. Here is a supplement: Strings are similar to character arrays in storage, so each single element of it can be extracted, such as s = " abcdefghij ", then s [1] =" b ", s [9] =" j ", and the zero bit of the string is exactly its length, such as s [0] = 10 ( The above function Ansistring does not.), This can provide us with many conveniences, such as each bit can be converted into a number and stored in an array during high-precision operations.
String
- Strings are mainly used for programming. For the description of concepts, function explanations, and usage details, see the text. Here's an additional point: Strings are similar in storage.
- A string or string is a string of characters consisting of numbers, letters, and underscores. It is generally written as s = a1a2 ... an (n> = 0). It is a data type that represents text in a programming language. In programming, a string is a continuous sequence of symbols or values, such as a string of symbols (a string of characters) or a string of binary numbers (a string of binary numbers).
- Generally, the entire string is used as the operation object, such as: searching for a substring in a string, finding a substring, inserting a substring at a certain position of the string, and deleting a substring. The necessary and sufficient conditions for the two strings to be equal are: the length is equal, and the characters at each corresponding position are equal. Let p and q be two strings. The operation of finding the first occurrence of q in p is called pattern matching. The two most basic storage methods for strings are sequential storage and link storage.
- 1. The connection operation concat (s1, s2, s3 ... sn) is equivalent to s1 + s2 + s3 + ... + sn.
- Example: concat ('11 ',' aa ') =' 11aa ';
- 2. Find the substring. Copy (s, I, I) truncates a substring of length l after the beginning of the I character from the string s.
- Example: copy ('abdag', 2,3) = 'bda'
- 3. Delete the substring. The procedure Delete (s, I, l) deletes the substring of length l after the start of the first character from the string s.
- Example: s: = 'abcde'; delete (s, 2,3); result s: = 'ae'
- 4. Insert the substring. Insert (s1, s2, I) inserts s1 into the ith position of s2
- Example: s: = abc; insert ('12', s, 2); result s: = 'a12bc'
- 5. Find the length of the string length (s) Example: length ('12abc') = 5
- Use len (s) to find the length of a string in ASP. Example: len ("abc12") = 5
- 6. Search for the position of the substring pos (s1, s2) If s1 is a substring of s2, return the position of the first character of s1 in s2, if not a substring, return 0.
- Example: pos ('ab', '12abcd') = 3
- 7. Uppercase conversion of characters. Upcase (ch) Find the upper case of the character ch.
- Example: upcase ('a') = 'A'
- 8. The value is converted to a number string. The procedure Str (x, s) converts the value x into a number string s.
- Example: str (12345, s); result s = '12345'
- 9. The number string is converted to a value. The process val (s, x, I) converts the number string s into the value x. If successful, I = 0, if unsuccessful, I is the ordinal of the invalid character, and the third parameter may not be passed.
- Example: val ('1234', x, I); result x: = 1234
- "Trojan.String.a" virus: vigilance ,
- Let be a non-empty called alphabet
- "Concatenation" is an important binary operation on *. For two strings s and t in *, their concatenation is defined as the sequence of characters in s followed by the sequence of characters in t and is indicated as st. For example, = {a, b,, z}, and s = bear and t = hug, then st = bearhug and ts = hugbear.
- String concatenation is associative, but not commutative. The empty string acts as a unit; for any string s, there is s = s = s. Therefore, the set * and the concatenation operation form a unitary semigroup, which is a free unitary semigroup generated from . In addition, the length function defines homomorphisms of unitary groups from * to non-negative integers.
- The string s is called the "substring" or "factor" of the string t, and if present (may be empty) the strings u and v make t = usv. The "is its substring" relationship defines a partial order on *, where
- It is often necessary to define the order on a collection of strings. If the character table has a total order (cf. alphabetical order), you can define a total order called lexicographic order on *. Note that because is finite, you can always define a good order on and then on *. For example, if = {0, 1} and 0 <1, then the lexicographic order of * is <0 <00 <000 <... <011 <0110 <... <01111 <... <1 <10 <100 <... < 101 < <111
- In formal theory, some extra operations on strings often occur. They are given in entry string operations.
- String data type is
- Although the formal string can have any (but limited) length, the length of a string in an actual language is often limited to an artificial maximum. Generally speaking, there are two types of string data types: "fixed-length strings", which have a fixed maximum length and use the same amount of memory regardless of whether the maximum value is reached; and "variable-length strings" ", Its length is not arbitrary and depends on the actual size and uses a variable amount of memory. Most strings in modern programming languages are variable-length strings. Despite the name, all variable-length strings have a limit in length. Generally, this limit depends only on the amount of memory available.
- Historically, the string data type assigned one for each character
- Some languages, such as C ++, implement strings as templates that can be used for any basic type, but this is an exception rather than a rule.
- If one
- A common notation is to use a character code
- Some programming languages are designed to make it easier to write string handlers. Here are some examples:
- awk
- Icon
- perl
- MUMPS
- sed
- SNOBOL
- Many UNIX utilities do simple string processing and can be used to simply write some powerful string processing algorithms. Files and finite streams can be viewed as strings.
- Some new programming languages, including Perl, Python, and Ruby,
- A simple string operation is "connection": that is to say, write a string S first, and then write a T later to get ST. Other common operations include searching for a substring in a long string, arranging a set of strings, and parsing a string. Because there are so many ways to apply strings, there are correspondingly many related algorithms that weigh different applications. Advanced string algorithms often use complex mechanisms and data structures including backward trees and finite state machines.
- Here are some string processing algorithms that perform different processing on strings:
- String search algorithm
- string is a series of characters. In PHP, characters are the same as bytes, which means that there are 256 different character possibilities. This also implies that PHP does not have native support for Unicode. See functions utf8_encode () and utf8_decode () for Unicode support
- Note: There is no problem if a string becomes very huge. PHP does not impose an implementation range on the size of the string, so there is no reason to worry about long strings.
- grammar
- Strings can be defined in three literal ways.
- apostrophe
- Double quotes
- Delimiter
String single quotes
- The easiest way to specify a simple string is to enclose it in single quotes (characters').
- To represent a single quote, you need to escape it with a backslash (/), as in many other languages. If a backslash is required before single quotes or at the end of a string, it must be represented by two backslashes. Note that if you try to escape any other characters, the backslash itself will also be displayed! So it is usually not necessary to escape the backslash itself.
- Note: In PHP 3, an E_NOTICE level warning will be issued in this case.
- Note: Unlike the other two syntaxes, variables and escape sequences that appear in single quoted strings are not replaced by the value of the variable.
- <? phpecho 'this is a simple string'; echo 'You can also have embedded newlines instrings this way as it isokay to do'; // Outputs: Arnold once said: "I'll be back" echo 'Arnold once said: "I / 'll be back"'; // Outputs: You deleted C: /*.*? Echo 'You deleted C: //*.*?'; // Outputs: You deleted C: /*.*? echo 'You deleted C: /*.*?'; // Outputs: This will not expand: / na newlineecho 'This will not expand: / na newline'; // Outputs: Variables do not $ expand $ eitherecho 'Variables do not $ expand $ either ';?>
- Also, if you try to escape any other characters, the backslash itself will be displayed!
- The most important thing about double quoted strings is that the variable names are replaced by the variable values. See String Parsing for details.
String delimiter
- Another way to delimit strings is to use the delimiter syntax ("<<<"). You should provide an identifier after <<<, then a string, and then the same identifier ends the string.
- The end identifier must begin in the first column of the row. Similarly, identifiers must follow the naming rules for any other tags in PHP: they can only contain alphanumeric underscores, and they must begin with an underscore or a non-numeric character.
- Example 11-3. Illegal example
- <? phpclass foo {public $ bar = <<< EOTbarEOT;}?>
- Delimiter text behaves just like a double-quoted string, but without double quotes. This means that there is no need to escape quotes in the delimiter text, but you can still use the escape codes listed above. Variables are expanded, but care must be taken when expressing complex variables in delimiter text as well as strings.
- Example 11-4. Delimiter string example
- <? php $ str = <<< EODExample of stringspanning multiple linesusing heredoc syntax.EOD; / * More complex example, with 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;?> Note: fixed Delimiter support was added in PHP 4.
String variable parsing
- When you specify a string with double quotes or delimiters, the variables in it are parsed.
- There are two syntaxes, one simple and one complex. Simple syntax is the most versatile and convenient. It provides methods for parsing variables, array values, or object properties.
- Complex syntax was introduced in php 4. You can enclose an expression in curly braces.
- If the simple syntax encounters a dollar sign ($), the parser will fetch as many characters as possible to form a valid variable name. If you want to indicate the end of the specified name, enclose the variable name in curly braces.
- <? php $ beer = 'Heineken'; echo "$ beer's taste is great"; // works, "'" is an invalid character for varnamesecho "He drank some $ beers"; // won't work,' s' is a valid character for varnamesecho "He drank some $ {beer} s"; // worksecho "He drank some {$ beer} s"; // works?>
- It is also possible to parse array indexes or object attributes. For array indexes, the closing square bracket (]) marks the end of the index. Object properties apply the same rules as simple variables, although there are no tricks for object properties like variables.
- <? php // These examples are specific to using arrays inside of strings.// When outside of a string, always quote your array string keys // and do not use {braces} when outside of strings either.// Let's show all errorserror_reporting (E_ALL); $ fruits = array ('strawberry' => 'red', 'banana' => 'yellow'); // Works but note that this works differently outside string-quotesecho "A banana is $ fruits [banana ]. "; // Worksecho" A banana is {$ fruits ['banana']}. "; // Works but php looks for a constant named banana first // as described below.echo" A banana is {$ fruits [ banana]}. "; // Won't work, use braces. This results in a parse error.echo" A banana is $ fruits ['banana']. "; // Worksecho" A banana is ". $ fruits [ 'banana']. "."; // Worksecho "This square is $ square-> width meters broad."; // Won't work. For a solution, see the complex syntax.echo "This square is $ square- > width00 centimeters broad. ";?>
- For any more complicated cases, complex syntax should be used.
- Complex (curly braces) syntax is not called complex because of its complex syntax, but because it can contain complex expressions.
- In fact, you can use this syntax to include any value in the namespace in the string. Just write an expression the same way as you would outside a string, and then include it with {and}. Because "{" cannot be escaped, this syntax is only recognized when $ immediately follows {(use "{/ $" or "/ {$" to get a literal "{$"). It can be clearer with some examples:
- <? php // Let's show all errorserror_reporting (E_ALL); $ great = 'fantastic'; // No, the output is: This is {fantastic} echo "This is {$ great}"; // Yes, the output is: This is fantasticecho "This is {$ great}"; echo "This is $ {great}"; // Worksecho "This square is {$ square-> width} 00 centimeters broad."; // Worksecho "This works: {$ arr [4] [3]} "; // This is wrong for the same reason as $ foo [bar] is wrong // outside a string. In otherwords, it will still work but // because php first looks for a constant named foo, it will // throw an error of level E_NOTICE (undefined constant) .echo "This is wrong: {$ arr [foo] [3]}"; / Works. When using multi-dimensional arrays, always use / / braces around arrays when inside of stringsecho "This works: {$ arr ['foo'] [3]}"; // Works.echo "This works:". $ arr ['foo'] [3]; echo " You can even write {$ obj-> values [3]-> name} "; echo" This is the value of the var named $ name: {$ {$ name}} ";?>
- Access and modify characters in a string
- Characters in a string can be accessed and modified by specifying the zero-based offset of the desired character with curly braces after the string.
- Note: For backward compatibility, square brackets can still be used. However this syntax is obsolete since php 4.
- Example 11-5. Some string examples
- <? php // Get the first character of a string $ str = 'This is a test.'; $ first = $ str {0}; // Get the third character of a string $ third = $ str {2}; // Get the last character of a string. $ Str = 'This is still a test.'; $ Last = $ str {strlen ($ str) -1}; // Modify the last character of a string $ str = ' Look at the sea '; $ str {strlen ($ str) -1} =' e ';?>
String utility functions
- Strings can be concatenated with the "." (Dot) operator. Note that the "+" (addition) operator cannot be used here. For more information, see String Operators.
- There are many utility functions for changing strings.
- See the section on string functions for general functions, and regular expression functions for advanced search and replace (two styles: Perl and POSIX extensions).
- There are also URL string functions, and functions to encrypt / decrypt strings (mcrypt and mhash).
- Finally, if you still can't find the function you want, see Character Type Functions.
String conversion
- You can use the (string) flag or the strval () function to convert a value to a string. When an expression requires a string, the conversion of the string is done automatically within the scope of the expression. For example when using the echo () or print () function, or when comparing a variable value to a string. Reading the sections on genres and genre tricks in the manual helps to be clearer. See settype () .
- The Boolean value TRUE will be converted to the string "1" and the value FALSE will be represented as "" (ie, an empty string). This makes it easy to compare between booleans and strings.
- When an integer or floating-point number is converted to a string, the string consists of numeric characters that represent those values (floating-point numbers also include an exponent part).
- The array will be converted to the string "Array", so the contents of the array cannot be output by the echo () or print () function. Please see below for more tips.
- The object will be converted to the string "Object". If you need to print out the member variables of the object for debugging purposes, please read below. If you want to get the name of the class to which the object is attached, use the function get_class () . As of PHP 5, the __toString () method can be used if appropriate.
- The resource type is always converted to a string in the format "Resource id # 1", where 1 is the unique identifier that php assigns to the resource at runtime. If you want to get the type of a resource, use the function get_resource_type () .
- NULL will be converted to an empty string.
- As shown above, printing out an array, object, or resource does not provide any useful information about the values themselves. See functions print_r () and var_dump () , these are better ways to print values for debugging.
- You can convert php values to strings to store them permanently. This method is called serialization and can be done with the function serialize () . If WDDX support is established when PHP is installed, you can also serialize PHP values into XML structures.
String conversion value
- When a string is evaluated as a number, the type and value of the result are determined according to the following rules.
- If you include any of the characters ".", "E", or "E", the string is evaluated as a float . Otherwise it is treated as an integer.
- The value is determined by the first part of the string. If the string starts with valid numeric data, use that number as its value, otherwise its value is 0 (zero). Legal numeric data starts with an optional sign, followed by one or more digits (optionally including decimal fractions), followed by an optional exponent. The exponent is an "e" or "E" followed by one or more numbers.
- <? 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 +" 10 Small Pigs "; // $ foo is integer (11 ) $ foo = 4 + "10.2 Little Piggies"; // $ foo is float (14.2) $ foo = "10.0 pigs" + 1; // $ foo is float (11) $ foo = "10.0 pigs" + 1.0; // $ foo is float (11)?>
- See the strtod (3) section of the Unix manual for more information on this conversion.
- If you want to test any of the examples in this section, you can copy and paste these examples and add the following line to see for yourself what happens:
- <? phpecho "/ $ foo == $ foo; type is". gettype ($ foo). "<br /> / n";?>
- Don't expect to get the encoding of a character when you convert it to an integer (and probably do the same in C). If you want to convert between character encoding and characters, use the ord () and chr () functions.