PHP built-in string handling functions

Source: Internet
Author: User
Tags md5 hash strcmp

Character of the string
1. Other types of data are used in string type processing functions and are automatically converted to strings after processing

<? PHP Echo substr ("ABCDEFGHIJKLMN", 2,4), "<br>";   // cdef//using numbers is automatically converted to strings Echo substr (123456,2,4);    // 3456?>

2. You can think of a string as an array, as a character set

 <? php   $str  = "ABCDEFG" ;  //  echo   $str  [2]. " <br> ";  //  but in order to differentiate between arrays we often use the following  echo   $str  {2}. " <br> "? 

Powerful built-in string handling functions
1. Commonly used string output functions
Echo ()
Print ()
Die ()----exit ()
printf () Formatting strings
sprintf () returns a formatted string

2. Commonly used string formatting functions

Remove characters
LTrim (); Remove the left string (the default is to remove the space)
RTrim (); Remove the right string
Trim (); Remove the strings on both sides

<?PHP$str= "ABC";Echo strlen($str)." <br> ";Echo strlen(LTrim($str))." <br> ";Echo strlen(RTrim($str))." <br> ";Echo strlen(Trim($str))." <br> ";$str 1= "123This is Test";//The second parameter specifies the character to delete (string)Echo LTrim($str 1, ' 1 '). " <br> ";//Remove all numbers 0..9 to indicate rangeEcho LTrim($str 1, ' 0..9 '). " <br> ";?>

Add a String

Str_pad (); Add a string (default is added to the right)

 <? php   $str  = "Hello"  //  from the right. echo  str_pad  ( $str , 10, "@"). " <br> ";  //  Both sides complement  echo  str_pad  ( $str , "@", Str_pad_both). " <br> ";  //  from the left echo  str_pad  ( $str , "@", str_pad_left). " <br> "? 

Uppercase and lowercase conversions
Strtolower (); All characters are converted to lowercase
Strtoupper (); All characters are converted to uppercase.
Ucfirst (); Capitalize the initial letter
Ucword (); Capitalize the first letter of each word

<? PHP $str= "My name is tom!" ; Echo Strtoupper ($str). " <br> "; Echo Strtolower ($str). " <br> "; Echo Ucfirst ($str). " <br> "; Echo Ucwords ($str). " <br> ";? >


String formatting related to HTML tags
NL2BR (); The function inserts an HTML line break (<br/>) before each new line (\ n) in the string.
Htmlentities (); The function converts the character to an HTML entity.
Htmllspeciachars (); function to convert some of the predefined characters into HTML entities.
The predefined characters are:
& (and number) becomes &amp;
"" (double quotes) become &quot;
"(single quotes) become & #039;
< (less than) becomes &lt;
> (greater than) becomes &gt;
Stripslashes (); The function removes the backslash added by the addslashes () function.
The Addslashes () function adds a backslash before the specified predefined character.
These predefined characters are:
Single quotation mark (')
Double quotation marks (")
Back slash (\)
Null

Strip_tags (); The function strips HTML, XML, and PHP tags.

<form>input: <input type= "text" name= "str" size= "> <input type=" Submit "Name=" sub "value=" Submit "> <b r></form><?PHPEcho $_get["str"]. " <br> ";//The function converts the character to an HTML entity. Echo htmlentities($_get["str"],ent_noquotes). " <br> ";//function to convert some of the predefined characters into HTML entities. Echo Htmlspecialchars($_get["str"]). " <br> ";//remove the \ That is added by the addslashes () function.Echo stripslashes($_get["str"]). " <br> ";//combined UseEcho htmlentities(stripslashes($_get["str"]). " <br> ";//the function strips HTML, XML, and PHP tags. Echo Strip_tags($_get["str"]). " <br> ";?>


Number_format (); function to format a number by using a thousands grouping.

<? PHP $a=1000000.12345; Echo $a. " <br> "; Echo Number_format ($a). " <br> "; // The decimal point is reserved for three bits, the thousand points are separated by ",", and the decimal point is "." Echo Number_format ($a, 3, '. ', ', '). " <br> ";? >


Strrev (); Function reversal String

<? PHP $str= "Hello world!" ; Echo $str. " <br> "; Echo Strrev ($str). " <br> ";? >

MD5 ();
The MD5 function computes the hash of the string.
The MD5 () function uses RSA data security, including the MD5 message selected passage algorithm.
If successful, returns the computed MD5 hash, or False if it fails.

<? PHP $pass= "1234#!_56"; if (MD5($pass) = = "A8affa3d7aca3a35a39f674e1e5f0cc7"    ) Echo "Ok!" ;? >


Md5_file ();
The function computes the MD5 hash of the file.
The MD5 () function uses RSA data security, including the MD5 message selected passage algorithm.
If successful, returns the computed MD5 hash, or False if it fails.

3. String comparison function
strcmp (); A function compares two strings.
0-If two strings are equal
<0-if string1 is less than string2
>0-if string1 is greater than string2
STRCASECMP ();
STRNATCMP ();

<?PHP$str 1= "ABCD";$str 2= "ABCD";if(strcmp($str 1,$str 2) ==0){    Echo' $str 1= $str 2 ';}ElseIf(strcmp(STR1,$str 2) >0){    Echo' $str 1> $str 2 ';}Else{    Echo' $str 1< $str 2 ';}?>

PHP built-in string handling functions

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.