PHP Common strings String Function instance Summary "conversion, substitution, calculation, interception, encryption" _php tips

Source: Internet
Author: User
Tags md5 md5 encryption rtrim strlen trim

The example in this article summarizes the common PHP string function. Share to everyone for your reference, specific as follows:

Nl2br

Function: Change line character to <br>

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

RTrim

Function: Clear the blank on the right

<?php
$str = "Hello World";
echo strlen ($STR). " <br> ";
$result = RTrim ($STR);
echo strlen ($result);
/** Results


Strip_tags

Features: Clears HTML and PHP tags from strings

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

Strtolower and Strtoupper

Features: Converting to case

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

Trim

Function: Remove the trailing space

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


Str_ireplace

Features: Replacing

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

Str_repeat

Function: Repeat a string more than once

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

Str_replace

Features: case-sensitive substitution

<?php
$str = "Hello jiqing!";
$result 1 = str_ireplace ("Hello", "Hi", $str); Case-insensitive
$result 2 = str_replace ("Hello", "Hi", $str);//Case Sensitive
echo $str. " <br> ";
echo $result 1. " <br> ";
echo $result 2. " <br> ";
/** results
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
$result 2 = Str_word_count ($str, 1);//return array
echo $str. " <br> ";
echo $result 1. " <br> ";
Print_r ($result 2);
/** results
Hello jiqing a!
3
Array ([0] => Hello [1] => jiqing [2] => a)
* * *

Strlen

Function: Return string length

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

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: Replace 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 one position, replacing several string
echo $result;
/** results
Hello zhangsan
hello zhangsan, hello jim!
*/

Substr

function: Get substring

<?php
$str = "abcdef";
$result = substr ($str, 0, 1); Starting from the No. 0, get 1
echo $result. " <br> ";
$result = substr ($str, 0,-1);//From the No. 0, gets the echo $result except the last string
. " <br> ";
$result = substr ($str, 2,-1);//From the 2nd, gets the echo $result except the last string
. " <br> ";
$result = substr ($str, -3,-1);//starting at 3, gets the string echo $result except the last one
. <br> ";
$result = substr ($str, -3,1);//starting at 3, gets the echo $result except the last string
. " <br> ";
/** result
a
abcde
cde
de
d
* *

Implode

Function: Converts an array to a string

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

Md5

Functions: MD5 encryption of strings

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

More interested in PHP related content readers can view the site topics: "PHP string (String) Usage summary", "PHP Array" Operation Techniques Encyclopedia, "PHP Basic Grammar Primer", "PHP Operations and Operator Usage Summary", " Introduction to PHP object-oriented programming program, "PHP Network Programming Skills Summary", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Related Article

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.