String functions

String functions

String functions –strcmp(), strcasecmp()

strcmp()strcasecmp() both returns zero if the two strings passed to the function are equal. These are identical, with the exception that the former is case-sensitive, while the latter is not.

  • Output – 1bc
  • Multiple-character version

  • Output- onetwo3

String functions –strpos()

  •  strpos() allows you to find the position of a substring inside a string. It returns either the numeric position of the substring’s first occurrence within the string, or false if a match could not be found.
  • You can also specify an optional third parameter to strpos() to indicate that you want the search to start from a specific position within the haystack.

  • Output – 


String functions –stripos() , strrpos()
 stripos() is case-insensitive version of strpos().

Output- 0

Strpos() does the same as strpos(), but in the revers order.

Output- 3

String functions –strstr()
The strstr() function works similarly to strpos() in that it searches the main string for a substring. The only real difference is that this function returns the portion of the main string that starts with the sub string instead of the latter’s position:

Output- 3456
String functions –stristr()
stristr() is case-insensitive version of strstr().

Output- My World
String functions –str_replace(), str_ireplace()
str_replace() used to replace portions of a string with a different substring.

Output- Hello Reader
Str_ireplace() is the case insensitive version of str_replace().

Output- Hello Reader
Optionally, you can specify a third parameter, that the function fills, upon return, with the number of substitutions made:

Output- 3
If you need to search and replace more than one needle at a time, you can pass the first two arguments to str_replace() in the form of arrays

Output – Hello Reader

Output – Bye Bye
String functions –substr()
The very flexible and powerful substr() function allows you to extract a substring from a larger string.

String functions –number_format()
Number formatting is typically used when you wish to output a number and separate its digits into thousands and decimal points.


Output- 100 000,698

Related Post