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