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.

$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.

$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.

$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
    echo strstr (’abc’, ’a’, ’1’);

    Output – 1bc

  • Multiple-character version
    $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.
    $haystack = ’123456123456’;
    $needle = ’123’;
    echo strpos ($haystack, $needle);
    echo strpos ($haystack, $needle, 1);

    Output – 


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

echo stripos(’Hello World’, ’hello’);

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

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:

$haystack = ’123456’;
$needle = ’34’;
echo strstr ($haystack, $needle);

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

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.

echo str_replace("World", "Reader", "Hello World");

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

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:

$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

echo str_replace(array("Hello", "World"), array("Bonjour", "Monde"), "HelloWorld");

Output – Hello Reader

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.

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

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy