String functions commonly used in PHP

Source: Internet
Author: User

Common string Functions in PHP and how to manipulate them:

1. Determine the length of a string
$str = "asdfghjklzxcvbnm,";
echo strlen ($STR);

2. Retrieving characters specified within a string
$str = "ABCDEFG";
Echo Strpos ($str, ' G ');

3. Remove both spaces and special character trim functions
$str = ' asdf d f g h ggg ';
Echo $str;
Echo ' <br> ';
echo Trim ($STR);
Remove left-side spaces and special characters LTrim functions
Echo LTrim ($STR);
Remove right-end spaces and special characters RTrim functions
echo RTrim ($STR);

2. Get the string length
$str = ' ASDFGHJKL ';
Echo substr ($str, 0,4);
Echo ' <br> ';
Echo substr ($STR, 2,4);
Echo ' <br> ';
Echo substr ($str, -5,4);
Echo ' <br> ';
Echo substr ($str, -5,-3);

3. Comparing strings
$str 1= ' asdf ';
$str 2= ' ZXCVB ';
Echo (strcmp ($str 1, $str 2));

In the order in which letters appear, they are case insensitive.
$str 1= ' ASD ';
$str 2= ' qwe ';
Echo strcasecmp ($str 2, $str 1);

Compare the first few characters
$str = ' ASDFG ';
$str 1= ' Asdert ';
Echo strncmp ($str 1, $STR, 3);

Compare by natural number size
$str 1 = ' 10 ';
$str 2 = ' 3 ';
Echo strnatcmp ($str 2, $str 1);

Find string
$str = ' AASSDDFFGGHHJJ ';
Returns all strings that match the string to the end of a condition
Echo strstr ($str, ' G ');

Unlocks the number of occurrences of a specified string
$str = ' asdfghasdfghasdfghjsdfghjasdfghj ';
Echo Substr_count ($str, ' f ');

Substitution of strings
$str = ' AA SS DD ff GG ';
Echo $str;
Echo ' <br> ';
echo str_ireplace (' dd ', ' DD ', $STR, $UNM);
Echo ' <br> ';
Echo $unm;
Echo ' <br> ';
Replaces a specified length of character from the specified index location
Echo substr_replace ($str, ' TTT ', 2,8);

String formatting
$str = ' 10000000000000000 ';
echo Number_format ($STR);
Echo ' <br> ';
Echo Number_format ($STR, 4);
Echo ' <br> ';
Echo Number_format ($STR, 4, ', ', ' _ ');

Split string
$str = ' a+a+s+g+h+l+k ';
$arr =explode (' + ', $str);
foreach ($arr as $item)
{
Echo ($item. ' ‘);
}
Merging strings
$arr =array ("AA", "SS", "DD", "FF", "GG");
$str =implode ($arr);
Echo $str;

String functions commonly used in PHP

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.