PHP String Functions

1] The addcslashes() function:

This function puts a backslash in front of a specific character in the string.

Example:

<?php 
$m = "Errorsea";
$n = addcslashes($m,"E");
echo $n;        //result: "\Errorsea"
?>

Note: This function is case sensitive.

2] The addslashes() function:

This function puts a backslash in front of some predefined characters. For example, a single quote, double quote, backslash, and NULL character.

Example:

<?php 
$m = addslashes("Do you visit 'Errorsea' during yourengineering ?");
echo $m; //result: "Do you visit \'Errorsea\' during your engineering?"
?>

3] The bin2hex() function:

This function converts the string from ASCII characters into equivalent hexadecimal values.

Example:

<?php 
$m = bin2hex("Errorsea");
echo($m);     //result: 4572726f72736561
?>

4] The chop() function:

This function is used for removing predefined characters and white spaces from the right end of the string.

Example:

<?php
$m = "Errorsea is good for learning.";
echo chop($m,"learning."); // result: "Errorsea is good for"
?>

5] The chr() function:

This function converts the ASCII value into the equivalent character.

Note: The ASCII values can be in the form of decimal, octal, or hexadecimal.

Example:

<?php
echo chr(69) . "<br>";      // Decimal , result: "E"
echo chr(052) . "<br>";     // Octal , result: "*"
echo chr(0x743) . "<br>";   // Hex ,result: "C"
?>

6] The chunk_split() function:

This function converts the string into a series of smaller parts.

Example:

<?php
$m = "Errorsea";
echo chunk_split($m,1,","); // result: "E,r,r,o,r,s,e,a,"
?>

7] The convert_cyr_string() function:

This function is used for converting a string from one Cyrillic character set into another.

Such as :

  • k – koi8-r
  • w – windows-1251
  • i – iso8859-5
  • a – x-cp866
  • d – x-cp866
  • m – x-mac-cyrillic

Example:

<?php
$m = "Errorsea! æøå";
echo $m . "<br>";
echo convert_cyr_string($m,'w','k'); 
?> 

Note: Above code converts the string from character set w to k.

8] The convert_uudecode() function:

This function decodes the uuencoded strings.

Example:

<?php
$m = "(17)R;W)S96$` `" ; 
echo convert_uudecode($m);  // result: "Errorsea"
?> 

9] The convert_uuencode() function:

This function encodes the character strings.

Example:

<?php
$m = "Errorsea" ; 
echo convert_uuencode($m);  // result: "(17)R;W)S96$` `"
?> 

10] The count_chars() function:

This function returns the information about the character used in the string.

Example:

<?php
$m = "Errorsea";
echo count_chars($m,3);
?> 

Note: The parameter “mode 3” will return a string with all the different characters used.

Also Read: What is JavaScript Hoisting?

11] The crc32() function:

This function is used for counting cyclic redundancy of a string.

Example:

<?php
$m = crc32("Errorsea");
printf("%u\n",$m);   // result: 2543775366
?>

12] The echo() function:

This function outputs one or more strings.

Example:

<?php
echo "Errorsea"; // result: "Errorsea"
?> 

13] The explode() function:

This function used for breaking the string into an array.

Example:

<?php
$m = "Errorsea is beautiful.";
print_r (explode(" ",$m));   
// result: Array ( [0] => Errorsea [1] => is [2] => beautiful. )
?>

14] The implode() function:

This function used returns string from an array.

Example:

<?php
$m = Array ( [0] => Errorsea [1] => is [2] => beautiful. );
echo (implode(" ",$m)); // result: "Errorsea is beautiful." 
?>

15] The hex2bin() function:

This function converts the string of hexadecimal values into equivalent ASCII characters.

Example:

<?php 
$m = hex2bin("4572726f72736561");
echo($m);     //result: "Errorsea"
?>

16] The join() function:

This function used returns string from an array.

Example:

<?php
$m = Array ( [0] => Errorsea [1] => is [2] => beautiful. );
echo join(" ",$m); // result: "Errorsea is beautiful." 
?>

17] The lcfirst() function:

This function converts the first character of string into lower case.

Example:

<?php 
echo lsfirst("Errorsea");   // result: "errorsea"
?>

18] The ltrim() function:

This function is used for removing predefined characters and white spaces from the left end of the string.

Example:

<?php
$m = "Errorsea is good for learning.";
echo ltrim($m,"Errorsea"); // result: "is good for learning."
?>

19] The rtrim() function:

This function is used for removing predefined characters and white spaces from the right end of the string.

Example:

<?php
$m = "Errorsea is good for learning.";
echo rtrim($m,"learning."); // result: "Errorsea is good for "
?>

20] The md5() function:

This function converts the string into equivalent md5 hash formate.

Example:

<?php
$m = "Errorsea";
echo md5($m); // result: f896a897d60fb4f5bbb641437fa995b1
?> 

21] The metaphone() function:

This function counts the metaphone keyword for the string.

Example:

<?php
echo metaphone("Errorsea"); // result: ERRS
?>

22] The nl2br() function:

This function puts <br> tag in place of \n in the string.

Example:

<?php
echo nl2br("Errorsea.\nBest");
?>

23] The ord() function:

The ord() is the function used for returning the ASCII value of the first character in the string.

Example:

<?php
echo ord("Errorsea");  // result: 69
?>

24] The parse_str() function:

This function parse the string into different variable.

Example:

<?php
parse_str("web=Errorsea&age=23");
echo $web."<br>";   // result: web
echo $age;       // result: 23
?>

25] The print() function:

This function outputs one or more strings.

Example:

<?php
print "Errorsea"; // result: "Errorsea"
?> 

26] The printf() function:

This function outputs the formatted one or more strings.

Example:

<?php
$no = 1;
printf("Errorsea is %u website",$no);   
// result: "Errorsea is 1 website"
?> 

27] The sha1() function:

This function converts the string into equivalent sha1 hash formate.

Example:

<?php
$m = "Errorsea";
echo sha1($m);  
// result:41b12b63eed7ebd228932d59f79ee7677bc2e80e
?>

28] The similar_text() function:

This function is used for finding similarities between two strings.

Example:

<?php
echo similar_text("Hello Yash","Hello Nick");  // result: 6
?>

29] The soundex() function:

This function finds the soundex key for a string.

Example:

<?php
$m = "Errorsea";
echo soundex($m); // result: E662
?>

26] The sprintf() function:

This function outputs the formatted one or more strings to a variable.

Example:

<?php
$no = 1;
$m =sprintf("Errorsea is %u website",$no);   
echo $m;
// result: "Errorsea is 1 website"
?>

27] The str_ireplace() function:

This function relaces some character of one string with other character string.

Example:

<?php
echo str_ireplace("Yash","Nick","Hello Yash ! ");  
// result: "Hello Nick ! "
?>

28] The str_pad() function:

This function expands the string to a new length.

Example:

<?php
$m = "Errorsea";
echo str_pad($m,10,"!");  // result: "Errorsea!!" 
?>

29] The str_repeat() function:

This function repeats the string to a specific number of count.

Example:

<?php
$m = "Errorsea";
echo str_pad($m,3);  // result: "ErrorseaErrorseaErrorsea" 
?>

30] The str_replace() function:

This function relaces some character of one string with other character string.

Example:

<?php
echo str_replace("Yash","Nick","Hello Yash ! ");  
// result: "Hello Nick ! "
?>

31] The str_rot13() function:

This function converts the string into equivalent rot13 encoded formate.

Example:

<?php
$m = "Errorsea";
echo str_rot13($m);        // result:Reebefrn
?>

32] The str_suffle() function:

This function used for randomly shuffling the characters of the string.

Example:

<?php
echo str_shuffle("Errorsea");  // result: aEeorrrs
?>

33] The str_split() function:

This function splits the string into an array.

Example:

<?php
print_r(str_split("Errorsea"));
// result: Array ( [0] => E [1] => r [2] => r [3] => o [4] => r [5] => s [6] => e [7] => a )
?>

34] The str_word_count() function:

This function is used for counting the number of words in the string.

Example:

<?php
echo str_word_count("Errorsea is love"); // result: 3
?>

35] The strcasecmp() function:

This function compares two strings, and it isn’t case sensitive function.

Example:

<?php
echo strcasecmp("errorsea","Errorsea"); // result: 0 
?>

Note: If it returns, 0 means two strings are equal.

36] The strchr() function:

This function is used for searching the first occurrence of a string inside the other string.

Example:

<?php
echo strchr("Errorsea is the best","best");  // result: "best"
?>

37] The strcmp() function:

This function compares two strings, and it is case sensitive function.

Example:

<?php
echo strcmp("errorsea","errorsea"); // result: 0 
?>

Note: If it returns, 0 means two strings are equal.

38] The strcspn() function:

This function is used for finding the number of characters, including white space before a particular character in a string.

Example:

<?php
echo strcspn("Errorsea is love","l"); // result: 12
?>

39] The strlen() function:

This function returns the length of the string.

Example:

<?php
echo strlen("Errorsea"); // result: 8 
?>

40] The strrev() function:

This function reverses the string.

Example:

<?php
echo strrev("Errorsea");  // result: aesrorrE
?>

41] The strstr() function:

This function is used for the searching first occurrence of a string inside another string.

Example:

<?php
echo strstr("Errorsea is the best","best");  // result: "best"
?>

42] The strtolower() function:

This function converts the string to lower case.

Example:

<?php
echo strtolower("Errorsea");  // result: "errorsea"
?>

43] The strtoupper() function:

This function converts the string to upper case.

Example:

<?php
echo strtoupper("Errorsea");  // result: "ERRORSEA"
?>

44] The substr() function:

This function returns a substring of a particular string.

Example:

<?php
echo substr("Errorsea ",5); // result: "sea"
?>

45] The wordwrap() function:

This function is used for wrapping the string into different strings when it reaches a particular length.

Example:

<?php
$m = "I visit errorsea for improving my skills and for interview preparations";
echo wordwrap($m,15,"<br>\n");
?>

Output:

I visit
errorsea for
improving my
skills and for
interview
preparations

Leave a Reply

Your email address will not be published. Required fields are marked *