By far we have learnt some of string function, in this tutorial we will continue our discussion of string functions with some more important functions.
The most commonly used function is echo() , which is used for printing one or more strings/variable. You can use echo with or without parentheses.
For Ex.
<?php $str1="Pratik"; echo "Hello ".$str1; ?>
Output
Hello Pratik
Here echo merges two strings using .(dot) operator and if you have noticed we have not used parentheses for echo function.
This function will return a part of string specified in argument.
Syntax
substr(string,start,length)
string it’s a string or string variable from which you are trying to extract the substring.
start is the starting position of the string from where you want to extract substring. If the start position is positive number it will start looking for substring from specified position, if its negative it will start looking for substring at specified position from the end of string and if the start position is 0 it starts looking for substring from very first position.
Length specifies the length of substring. By default it is end of string.
For Ex.
<?php $str1="DiscussDesk"; echo substr($str1,7)."<br>"; echo substr($str1,0,7)."<br>"; echo substr($str1,-4,4)."<br>"; ?>
Output
Desk Discuss Desk
This function is used for comparing two strings. It returns 0 if both strings are same , positive number if string1 is alphabetically greater than string2 , and negative number if string2 is alphabetically greater than string1.
Syntax
strcmp(string1,string2)
Note that the comparison will be case sensitive that is small and capital letters will be considered distinct.
This function is also used for comparing two strings the only difference is that the comparison will be case insensitive meaning small and capital letters will be considered equal.
Syntax
strcasecmp(string1,string2)
For Ex.
<?php $str1="DiscussDesk"; $str2="discussdesk"; echo strcmp($str1,$str2)."<br>"; //notsame echo strcasecmp($str1,$str2)."<br>"; //same ?>
Output
-1 0
Repeats a specified string number of times.
Syntax
str_repeat(string,repeat)
string specifies the string to be repeated.
repeat specifies number of times the string to be repeated.
For Ex.
<?php $str1="Pratik"; echo str_repeat($str1,3); // repeats the string 3 times ?>
Output
PratikPratikPratik
It’s abbreviated as string translate. It replaces a set of characters in a given string with specified set of characters.
Syntax
strtr(string,from,to)
string string to translate.
from specifies which characters to be translated.
to specifies which characters will be replaced.
For Ex.
<?php $str1="Pratik"; echo strtr($str1,"ai","@!"); ?>
Output
Pr@t!k
This function finds the position of first occurrence of the specified character of string in given string.
Syntax
strpos(sourceString,findString,start) (its case sensitive) stripos(sourceString,findString,start) (its case insensitive)
sourceString The string from which position to be find.
findString Specifies the string to be find.
start Specified from where to begin the search. By default start of string.
For Ex.
<?php $str1="Hello DiscussDesk, Hello world"; echo strpos($str1,"Hello",0)."<br>"; echo stripos($str1,"hello",5)."<br>"; ?>
Output
0 19
It’s one of the most frequently used string function. This function breaks the given string into an array specified by the separator.
Syntax
explode(separator,string)
separator specifies where to break the string.
string specifies the string to be split.
For Ex.
<?php $str1="Hello DiscussDesk Hello world"; print_r(explode(" ",$str1)); ?>
Output
Array ( [0] => Hello [1] => DiscussDesk [2] => Hello [3] => world )
It’s a reverse function of explode(). It returns a string from the elements of an array.
Syntax
implode(separator,array)
separator Specifies what do you want to put between the array elements when its converted into string.
array Specifies the array from which you need to make a string
For Ex.
<?php $arr=array("Hello","DiscussDesk","Hello","World"); echo implode(" ",$arr); ?>
Output
Hello DiscussDesk Hello World
its used for password and secure data encryption. Whenever used with any string it calculates the md5 hash of the given string.
For Ex.
<?php $str1="Pratik"; echo md5($str1); ?>
Output
7bccf0bdc2a136dd7bbeaf1d93822d93
This function is used to randomly shuffling the characters of the given string. Each time the output will be random.
Syntax
str_shuffle(string)
For Ex.
<?php $str1="Pratik"; echo str_shuffle($str1); ?>
Output
tikrPa
the output shows one possibility, it will generate any random string from the given set of characters in your string.
So to conclude we have covered almost all important string functions , which are very useful while you are working with database for creating dynamic websites.