PHP's Common string method

Source: Internet
Author: User

1.trim: Remove whitespace characters (or other characters) from the end of a string
eg
$text = "            There is a few words.       ";
Print_r (Trim ($text));
2.ltrim: Remove the left white space character.

3.rtrim: Remove the white space character to the right.
4.str_pad (): fills the string as required. The first argument is the string to fill, the second is the padding length, the third parameter is the fill symbol,
The fourth parameter is the direction of the fill (
Str_pad_left: String left fill.
Str_pad_right : String right fill.
Str_pad_both: String ends fill
)
eg
$input = "Alice.";

Echo Str_pad ($input, 10, "-="); alice.-=-=
Echo Str_pad ($input, "* *", str_pad_both);  alice.*****
5.strtolower (): string str all lowercase strings.
6.strtoupper (): This function converts the string str to all uppercase strings.

7.ucfirst (): The first character of the string is changed to uppercase.
8.ucwords (): string The first letter of each word is capitalized.
9.NL2BR (): Converts the string "\ n" to the HTML line break "<br/>"; the second parameter indicates whether to use XHTML-compatible line breaks
eg
Echo nl2br ("Welcome\r\nthis is my HTML document", false);//welcome
This is my HTML document
10.htmlspecialchars (): Turn the specified special symbols into entities, such as &lt;&gt;, etc.
eg
Echo htmlspecialchars ("<a href= ' # ' >Test</a>");//<a href= ' # ' >Test</a>
11.strip_tags (): Removes HTML and PHP tags from the string.
eg
$text 1 = "<p>test paragraph.</p><!--Comment--><a href= ' # ' >other text</a>";
echo $text 1;//test paragraph.
               Other text


12.strrev (): Reverses the string backwards. Reverses the string.
$text 2 = "Hello World";
Echo Strrev ($text 2);//dlrow Olleh


13.strlen (): Gets the string length.

14.number_format (): Format a number in thousands separators
eg
$number = 1234.56;
echo Number_format ($number);//1,235
echo  Number_format ($number, 2);//1,234.56
echo  Number_format ($number, 2, ",", "");//1234,56
15.MD5 (): cryptographic function
16.STRCMP (): Binary secure string comparison
eg
$var 1 = "Hello";
$var 2 = "Hello";
if (strcmp ($var 1, $var 2)!== 0) {
echo "$var 1 is not equal to $var 2 in a case sensitive string Comarison";
}
17.STRNCMP (): Binary security compares several characters at the beginning of a string, similar to strcmp, except that you can specify the length that is used when two string comparisons are made.

eg
$arr 1 = $arr 2 = Array ("Img12.png", "Img10.png", "Img2.png", "img1.png");

Usort ($arr 1, "strcmp");//array ([0] = img1.png [1] = = Img10.png [2] = = Img12.png [3] = = img2.png)
18.STRCASECMP (): Binary Security comparison string (case insensitive)
19.STRNATCMP (): Comparing strings using natural sorting algorithms
eg
$arr 1 = $arr 2 = Array ("Img12.png", "Img10.png", "Img2.png", "img1.png");

Usort ($arr 2, "strnatcmp");//array ([0] = img1.png [1] = = Img2.png [2] = = Img10.png [3] = = img12.png)
20.similar_text (): Calculates the similarity of two strings


PHP's Common string method

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.