PHP String Functions

PHP provides lots of string functions to implement various operations on strings. Strings are the most comman data type of any back-end language. So, we have to perform various operations on strings to convert them to our required output. This methods will help you to manipulate string data in PHP.

String Functions

String functions provides many pre-defined operations like concate, convert to UpperCase, convert to LowerCase, string to array, etc. Here, we have explained all the necessary string functions in PHP.

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." 
?>;

Read Also: What is implode function and how to use implode()

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

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
?>

30] The str_ireplace() Function

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

Example

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

31] The str_pad() Function

This function expands the string to a new length.

Example

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

32] 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" 
?>

33] The str_replace() Function

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

Example

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

34] The str_rot13() Function

This function converts the string into equivalent rot13 encoded format.

Example

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

35] The str_suffle() Function

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

Example

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

36] 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 )
?>

37] 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
?>

38] 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.

39] The strchr() Function

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

Example

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

40] 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.

41] 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
?>

42] The strlen() Function

This function returns the length of the string.

Example

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

43] The strrev() Function

This function reverses the string.

Example

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

44] 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"
?>

45] The strtolower() Function

This function converts the string to lower case.

Example

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

46] The strtoupper() Function

This function converts the string to upper case.

Example

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

47] The substr() Function

This function returns a substring of a particular string.

Example

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

48] 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

Conclusion

I hope now have a brief idea about the most of PHP string methods. Pease visit the PHP tutorial section for more information about the use of PHP string functions.

Leave a Reply

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