PHP string-related methods

Source: Internet
Author: User

1. Remove spaces and special character trim functions

$str = ' asdf ghjkl\t ';

Echo ($STR);
Trim Remove both spaces

Echo (Trim ($STR));
Echo (' Test ');

Remove the specified characters as and \ T from both ends
Echo (Trim ($str, ' as\t '));

$str 1= ' ASDFG ';
Remove the left space
Echo (LTrim ($str 1));

$str 2= ' lamco.com.cn ';
//
Echo (LTrim ($str 2, ' Lam '));

$str 3= ' www.lamco.com.cn ';
Remove the right space
Echo (RTrim ($str 3));
Echo (RTrim ($str 3, '. cn '));

2. Get the string length

$str 3= ' Hello Lamco ';
Echo (strlen ($str 3));

3. Intercepting strings
$str 4= ' http://www.lamco.com.cn ';

Echo (substr ($str 4,0,4));
Intercepts a string to the end of a string starting at the specified index position
Echo (substr ($str 4,7));

Echo (substr ($str 4,-5));

Counting from the end of the string, the second-to-last position of the Intercept
Echo (substr ($str 4,-5,-2));


4. Comparing strings

$str 1= ' Lisi1 ';

$str 2 = ' Lisi ';

-1 0 1

Echo (strcmp ($str 1, $str 2));

$str 1 = ' 10 ';
$str 2 = ' 2 ';
Compare case-insensitive in the order in which letters appear
Echo (strcasecmp ($str 1, $str 2));

Compare by natural number size
Echo (strnatcmp ($str 1, $str 2));

$str 3 = ' ASDFF ';
$str 4= ' asdfghj ';

Compare the first few characters
Echo (STRNCMP ($str 3, $str 4,3));

Find string

$str = ' http://www.welcome to lamco.com.cn ';
Returns the string of the compound condition to the end of all characters
Echo (Strstr ($str, ' www '));

$str = ' asdfghgfdsadasdf ';
Retrieves the number of occurrences of a specified string in a string
Echo (Substr_count ($str, ' a '));

$str = ' Welcome to Sdzysdzysdzy ';

Finds the characters in the specified string that need to be replaced.
1. The character to be replaced
2. Replaced characters
3. The string to query
4. Number of characters to replace

Echo (Str_ireplace (' Sdzy ', ' lamco.com.cn ', $str, $count));
Echo ($count);

Replace string

$str = ' Welcome to lamco.com.cn ';
Replaces a specified length of string from the specified index location
Echo (Substr_replace ($str, ' Lamco ', 0,7));

String formatting
Echo (Number_format (123456));

Echo (Number_format (123456,3));

Echo (Number_format (123456,3, ', ', '. '));

Split string

$str = ' a+s+d+f+g+h+k ';
//
$arr = explode (' + ', $str);
//
foreach ($arr as $item)
//{
Echo ($item. ' ‘);
//}

Composite string
$NEWSTR = Implode (' _ ', $arr);
Echo ($NEWSTR);

PHP string-related methods

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.