PHP commonly used string functions.

Source: Internet
Author: User
Tags md5 encryption

Nl2br
Function: <br> of line break

<?php$str = "cat isn ' t \ n Dog"; $result = NL2BR ($STR); echo $result;/** result cat isn ' tdog*/



RTrim
Function: Clear the blank on the right

<?PHP$STR = "Hello world   "; Echo strlen ($STR). " <br> "; $result = RTrim ($STR); Echo strlen ($result);/** results 1411*/




Strip_tags
Function: Clear the HTML and PHP tags in the string

<?PHP$STR = "<font color = ' Red ' >hello world</font>"; $result = Strip_tags ($STR); echo $result;/** result Hello world*/

Strtolower
Strtoupper
Functions: Converting to uppercase and lowercase

<?php$str = "Hello world!"; $result = Strtolower ($STR); Echo $result. " <br> "; $result = Strtoupper ($STR); echo $result;/** result Hello world! HELLO world!*/




Trim
Function: Remove the leading and trailing spaces

<?php$str = "  Hello world!  "; $result = Trim ($STR); Echo $str. " <br> "; Echo $result." <br> "; Echo strlen ($STR)." <br> "; Echo strlen ($result);/** result Hello world! Hello world!1612*/



Str_ireplace
function: Replace

<?php$str = "Zhang San"; $result = Str_ireplace ("Zhang", "Li", $str); Echo $str. " <br> "Echo $result/** results Zhang Sanli san*/



Str_repeat
Function: Repeat a string multiple times

<?php$str = "Hello jiqing!"; $result = Str_repeat ($str, 4); Echo $str. " <br> "; echo $result;/** result Hello jiqing! Hello jiqing! Hello jiqing! Hello jiqing! Hello jiqing!*/



Str_replace
Function: Case-sensitive substitution

<?php$str = "Hello jiqing!"; $result 1 = str_ireplace ("Hello", "Hi", $str); Case-insensitive $RESULT2 = Str_replace ("Hello", "Hi", $str);  Case-sensitive echo $str. " <br> "echo $result 1." <br> "Echo $result 2." <br> ";/** result Hello jiqing! Hi Jiqing!hello jiqing!*/


Str_word_count
Function: Returns the number of words in a string

<?php$str = "Hello jiqing a!"; $result 1 = str_word_count ($STR);    Number of returns $RESULT2 = Str_word_count ($str, 1);  Returns the array echo $str. <br> "echo $result 1." <br> ";p Rint_r ($result 2);/** result Hello jiqing a!3array ([0] = = Hello [1] = = jiqing [2] = a) */


Strlen
Function: Returns the string length

<?php$str = "Hello jiqing a!"; $result = strlen ($STR); echo $result;/** result 15*/


Substr_count
Function: Calculates the number of a string in another string

<?php$str = "Hello jiqing, hello jim!"; $result = Substr_count ($str, "Hello"); echo $result;/** result 2*/



Substr_replace
Function: Start a replacement from a location

<?php$str = "Hello jiqing, hello jim!"; $result = Substr_replace ($str, "Zhangsan", 6); Echo $result. " <br> "; $result = Substr_replace ($str," Zhangsan ", 6,6);//replace from a location, replace several strings echo $result;/** result Hello Zhangsanhello Zhangsan, Hello jim!*/.



Substr
function: Get substring

<?php$str = "ABCdef"; $result = substr ($str, 0, 1); Starting with the No. 0, get 1 echo $result. " <br> "; $result = substr ($str, 0,-1);//Starting from No. 0, get the echo $result except for the last string." <br> "; $result = substr ($str, 2,-1);//starting from 2nd, get the echo $result except for the last string." <br> "; $result = substr ($str, -3,-1);//starting from 3, get the echo $result except for the last string." <br> "; $result = substr ($str, -3,1);//starting from 3, get the echo $result except for the last string." <br> ";/** results aabcdecdeded*/

Implode
Function: Convert an array to a string

<?php$array = Array ("6", "3"), $date = Implode ("/", $array); echo $date;/** result 2013/6/3*/



Md5
Function: MD5 encryption of strings

<?PHP$STR = "Hello World"; $result = MD5 ($STR); echo $result;/** result 3e25960a79dbc69b674cd4ec67a72c62*/

PHP commonly used string 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.