Number, floating point, Boolean, string, and array (php Tutorial 1)

Source: Internet
Author: User
Tags ord php tutorial strlen

For example:

The code is as follows: Copy code
$ Fa = 3.14;

Strings and arrays are reference types. That is to say, they are placed in the stack as addresses. When a value is assigned again, the address in the stack changes direction. The original direction is lost or recycled: string and array.

For example: $ str = "string"; $ arr = array ("a" => "number", "B" => "group"); // array () it is an array assignment function. There are more than one thousand such functions in php, and less than 200 are commonly used, I think.

String operation:

Instance 2: string merging and addition
Copy the code as follows:

The code is as follows: Copy code
<? Php
$ Str = 1;
Echo $ str. = ""; // The number is converted to a string and then merged. Result: "1 ".
Echo "<br> ";
Echo $ str + = "1 RMB"; // The string is converted to a number and then added. For example, if "1xxx" is converted to a number 1, the result is 2.
Echo "<br> ";
?>

Instance 3: The string is case-insensitive.
Copy the code as follows:

The code is as follows: Copy code
<? Php
$ Str = "12345abc ";
Echo strtolower ($ str); // The result is "12345abc ".
Echo "<br> ";
Echo strtoupper ($ str); // small write, result: "12345abc ".
Echo "<br> ";
?>

Example 4: string length, substring truncation (Chinese and English)
Copy the code as follows:

The code is as follows: Copy code
<? Php
$ Str = "string 2 ";
Echo mb_strlen ($ str, "UTF-8"); // return the string length function. The second parameter is Encoding. This is because the page is encoded with UTF-8. If this parameter is omitted, the number of bytes in memory (ascii) is returned, that is, 10. Result 4:
Echo "<br> ";
Echo mb_substr ($ str, 1, 2, "UTF-8"); // returns the truncation of characters. 1 is the truncation starting from the "character" address, 2 is a string of 2 UTF-8 encoded characters ".
Echo "<br> ";
/**
* Knowledge Point: now you are new to functions. Each function has () as a stack call. () contains 0 or more parameters, which can be customized with default values. For example, echo does not have a keyword.
* Many books are encoded in gb2312 format. It is difficult to get the length and substring. The following is a reference to the implementation principle of the above mb Chinese character string extension Library:
*/
Function my_mb_strlen ($ str, $ code = "UTF-8") // defines a new function. $ str is a required parameter.
{$ Num = 0;
If ($ code = "UTF-8 ")
{
$ Str = iconv ("UTF-8", "gb2312", $ str); // Convert to gb2312 encoding. The ord function returns the corresponding ascii value to determine whether the Chinese character of each byte ends.
For ($ I = 0; $ I <strlen ($ str); $ I ++) // in this strlen ($ str) the number of bytes occupied by the returned memory is equivalent to mb_strlen ($ str)
{
If (ord ($ str [$ I])> 0xa0) $ I ++; // $ str [$ I] corresponds to the memory I bytes. If UTF-8 is used directly, it will be more complicated, because the diversity of encoding UTF-8 is commonly used for webpage encoding, and UTF-16 (unicode) is windows encoding.
$ Num ++;
}
}
Else
{
$ Num = "encoding not implemented ";
} // Check your information if you are interested.
Return $ num;
}
Echo my_mb_strlen ($ str ). ";". my_mb_strlen ($ str, "gb2312 "). "<br>"; // This page is encoded in UTF-8, but you can say that the input string 3 is gb2312, which cannot be correct even if the function is implemented.
?>

Instance 5: Search and replace substrings

Copy the code as follows:

The code is as follows: Copy code
<? Php
$ Str = "string 4 ";
Echo mb_strpos ($ str, 'string4', 0, "UTF-8"); // you can specify the position of the first substring from 0. The result is 2. If no value is found, null (= "") is returned. If the last two parameters are not found, 6 is returned.
Echo "<br> ";
Echo mb_strstr ($ str, 'string', 0, "UTF-8"); // extract the first substring found from 0 to the end. The result is "string 4 ". If no value is found, null (= "") is returned. If the last two parameters are not found, the same = strstr ($ str, 'string') is returned ').
Echo "<br> ";
Echo str_replace ("4", "not 4", $ str); // string replacement. Result: "The string is not 4 ".
Echo "<br> ";
?>

Example 6: empty and html escape of the substring
Copy the code as follows:

The code is as follows: Copy code
<? Php
$ Str = "string 5 ";
Echo $ str = trim ($ str); // remove spaces on both sides. Result: "string 5 ".
Echo "<br> ";
Echo "color =" red ""; // manually convert the preceding ', "to store it in the memory. The result is" color = "red ""
Echo "<br> ";
$ Str = "<br> 123 ";
Echo htmlentities ($ str); // string escape <> & '"avoids conflicts with html identifiers so that they can be displayed on the html browser. The result is:" <br> 123 ".
Echo "<br> ";
?>

 

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.