String functions
PHP String Functions
PHP String functions used to manipulate the strings.
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.
1 2 3 4 5 6 7 |
$str = "Hello World"; if (strcmp($str, "hello world") === 0) { // We won’t get here, because of case sensitivity } if (strcasecmp($str, "hello world") === 0) { // We will get here, because strcasecmp() is case-insensitive } |
String functions –strcasencmp()
strcasencmp() allows you to only test a given number of characters inside two strings.
1 2 3 4 |
$s1 = ’abcd1234’; $s2 = ’abcd5678’; // Compare the first four characters echo strcasencmp ($s1, $s2, 4); |
String functions –strlen()
strlen() is used to determine the length of a string.
1 2 |
$a="PHPGURUKUL Education Parnter"; echo strlen($a); |
Output – 28
String functions –strtr()
strtr() used to translate certain characters of a string into other characters.
- Single character version
123<?phpecho strstr (’abc’, ’a’, ’1’);?>
Output – 1bc - Multiple-character version
1234<?php$subst = array (’1’ => ’one’,’2’ => ’two’);echo strtr (’123’, $subst);?>
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.
123456<?php$haystack = ’123456123456’;$needle = ’123’;echo strpos ($haystack, $needle);echo strpos ($haystack, $needle, 1);?>
Output –
0
6
String functions –stripos() , strrpos()
stripos() is case-insensitive version of strpos().
1 |
echo stripos(’Hello World’, ’hello’); |
Output- 0
Strpos() does the same as strpos(), but in the revers order.
1 |
echo strrpos (’123123’, ’123’); |
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:
1 2 3 |
$haystack = ’123456’; $needle = ’34’; echo strstr ($haystack, $needle); |
Output- 3456
String functions –stristr()
stristr() is case-insensitive version of strstr().
1 |
echo stristr(’Hello My World’, ’my’); |
Output- My World
String functions –str_replace(), str_ireplace()
str_replace() used to replace portions of a string with a different substring.
1 |
echo str_replace("World", "Reader", "Hello World"); |
Output- Hello Reader
Str_ireplace() is the case insensitive version of str_replace().
1 |
echo str_ireplace("world", "Reader", "Hello World"); |
Output- Hello Reader
Optionally, you can specify a third parameter, that the function fills, upon return, with the number of substitutions made:
1 2 3 |
$a = 0; str_replace (’a’, ’b’, ’a1a1a1’, $a); echo $a; |
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
1 |
echo str_replace(array("Hello", "World"), array("Bonjour", "Monde"), "HelloWorld"); |
Output – Hello Reader
1 |
echo str_replace(array("Hello", "World"), "Bye", "Hello World"); |
Output – Bye Bye
String functions –substr()
The very flexible and powerful substr() function allows you to extract a substring from a larger string.
1 2 3 4 5 |
echo substr ($x, 0, 3);// outputs 123 echo substr ($x, 1, 1);// outputs 2 echo substr ($x, -2); //outputs 67 echo substr ($x, 1); //outputs 234567 echo substr ($x, -2, 1); //outputs 6 |
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