3. String (1)

Source: Internet
Author: User
Tags urlencode

 

Iii. String

1. Three methods of writing directly: (1) single quotes, variables are not replaced, and only \ '\ is supported \'\\
Two translated characters


(2) double quotation marks replace variables. Double quotation marks can be used \'
All escape characters


(3)HeredocFor example:

$ String =
<End_of_string

Haha hehe Hoho ....

Hehe

End_of_string;


The end_of_string can be replaced by any string. HeredocWill replace the variable. Characters such as single quotes, double quotes, and spaces are retained.

(4) http://hi.baidu.com/kkwtre/blog/item/bdb9cc35d1674a1291ef39fd.htm


Http://hi.baidu.com/kkwtre/blog/item/f5cdb4fa7521fcdcb58f312a.html

2. Echo:
It is not a function, but a language structure, so it cannot be used as an expression. For example, if (echo 'test') will cause parsing errors


3. Access a single character: $ STR = "Haha"; echo
$ STR {0 };// H


4. Delete blank spaces:Trim (string [, charlist]); Ltrim (), rtrim (); p82

<? PHP

$ String = "aaaabbbbbbaaaaaadddda ";

$ String1 =
Trim ($ string, "");// Remove both sides of

Echo $ string1; // bbbbbbaaaaaadddd

Echo $ string; // remain unchanged or aaaabbbbbbaaaadddda

?>

5.
Case sensitivity change:Strtolower () strtoupper ()
Operate the entire string,Ucfirst ()Operate only the first letter of the string,Ucwords ()Operate the first letter of each word

6. HTML encoding:Htmlentities () htmlspecialchars () html_entity_decode ()

Http://hi.baidu.com/kkwtre/blog/item/dbc2a2c4c0239cad8326ac23.html

7. Delete HTML tags:
Strip_tags ($ html_string, $ reserve_string );The second parameter specifies the tag to be left.

8. URL EncodingUrlencode (), urldecode (), rawurlencode (), rawurldecode ().

(1)
The difference between the two encoding methods is that space is processed. The former encoding is +, and the latter encoding is % 20.

(2)
Chinese characters are also encoded in the form of % DD.

(3)
Do not use these functions for a complete URL, because it will transfer the colon and backslash. only part of the URL should be encoded, and then add the protocol and domain name before

<? PHP

$ Name = "php
Program Design ";

$ Output =
Urlencode($ Name );

Echo "http://www.kkwtre.com/?#output }";

// Http://www.kkwtre.com/php+%B3%CC%D0%F2%C9%E8%BC%C6

?>

9. Comparison string: (1)
Comparison operator. <,> When a parameter of a comparison operator is a number, other parameters are also convertedNumeric type, So"Php" <1. "Php" is converted to 0.

And = comparison operator, when one is a number, the other is a string, convert the numberString.

(2)Strcmp ()Case Sensitive,
Strcasecmp ()Case-insensitive. Before comparison, the string is converted to lowercase.

(3)Strncmp(String1, string2,Len),Strncasecmp(String1, string2,Len) Only compare the first LEN characters

(4)
Strnatcmp (), strnatcasecmp ()Compare the natural order

10. Substring acquisition: $ piece =
Substr (string, start [, length]);

11.
String insertion and deletion:Substr_replace (orginal_string, new_string, start [, length]).

This function first deletes the length from start and then inserts new_string at start. This function has the following usage:

(1). Common Operations:

<? PHP

$ Greeting = "Good Morning citizen ";

Echo
Substr_replace ($ greeting, "bye", 5, 7 );// Good bye citizen

?>

(2). insert: insert without deletion. You only need to set the length to 0.

$ Greeting = "Good Morning citizen ";

// Good bye morning citizen

Substr_replace ($ greeting, "bye", 5, 0 );

(3). Delete: to delete a substring, you only need to set the second parameter new_string ""

Substr_replace ($ greeting, "", 5, 7); // good citizen

Substr_replace ($ greeting, "", 5); // good


In addition, both start and length can be negative values, indicating that the count starts from the end of the string.

12. String filling:Str_pad (original_string, to_length [, pad_string, str_pad_right | str_pad_right_left | str_pad_both]);

(1)
This function fills original_string with pad_string into a to_length string. The default value is str_pad_right, that is, the right side.

(2)
If no parameter is set for pad_string, a space is used by default to add it to the right of the string.

<? PHP

Echo str_pad ('hahaha', 30). "end"; // Haha end

Echo str_pad ('hahaha', 30 ,'. '). "end"; // Haha .......................... end

?>

13.
String flip:Strrev (string );

14.
A string consisting of repeated characters:Str_repeat (string, count );
For example, create a separator: str_repeat ('-', 30 );

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.