String Functions in PHP

String is used to handle a function it is a sequence of characters .There are some of the string with example.

1. addcslashes :-

It return the String with backslashes before the character.

<?php
echo addcslashes('Hello[ ]', 'A..z');
?>

Output

\H\e\l\l\o\[ \]

2. addslashes :-

As the name indicate it add the backslashes before the character that are large or liberated or leading character of a string.

<?php
$string = "O'My god your are totally change.";
echo addslashes($string);
?>

Output

O\'My god your are totally change.

3. bin2hex :-

It is used to convert the binary data in hexadecimal format  in byte wise  containing the largest-nibble first.

$binary = "11110000";
$hexa = dechex(bindec($binary));
echo $hexa;
?>

Output

f0

4. chop :-

It is used to delete the last character or space  from the right side of the string.

<?php
echo "<pre>";//without <pre> you cann't get correct output
echo chop(" Aroundthe ");
echo chop("Aroundtheworld", "a..z");
echo "</pre>";
?>

Output

AroundtheA
5. chr :-

It is used to return one character string in a single byte from a number.

<?php
echo chr(-159), chr(503), PHP_EOL;
?>

Output

a�

6. chunk-split :-

This function is used to divide (tear) the string into small block.

<?php
$string = "This is demo!";
echo chunk_split($string,2,".");
?>

Output

Th.is. i.s .de.mo.!.

7. convert_cyr_string :-

It is used to convert the string  from one cyrillic character to the another.

<?php
$string = "Good Morning have a nice day! وّه";
echo $string . "<br>";
echo convert_cyr_string($string,'m','a');
?>

Output

Good Morning have a nice day! وّه
Good Morning have a nice day! � � �

8. convert_uudecode :- 

It is used to convert the string decode to uuencoded.

<?php
$string = "A2&D@979E<GEO;F4@=&AI<R!I<R!A(&1E;6\@8VQA<W,N";
echo convert_uudecode($string);
?>

Output

Hi everyone this is a demo class.

9. count_chars :-

It counts the number that are pass through every byte and return information about the characters that are used in a String.

<?php
$str = "This is devstudioonline!";
echo count_chars($str,3);
?>

Output

!Tdehilnostuv

10. crc32 :-

It gives the result in crc32 polynomial of String.

<?php
$string = crc32("Hello Everyone!");
printf("%u\n",$string);
?>

Output

265055217

11. echo :-

It is used to give the output of the string.

<?php
echo "Hello World";
?>

Output

Hello World

12. explode :-

It crack the string by a string.

<?php
$text1 = "hello";
$text2 = "Students welcome back";
$text3 = ',';
var_dump( explode( ',', $text1 ) );
var_dump( explode( ',', $text2 ) );
var_dump( explode( ',', $text3 ) )
?>

Output

array(1) { [0]=> string(5) "hello" } array(1) { [0]=> string(21) "Students welcome back" } array(2) { [0]=> string(0) "" [1]=> string(0) "" }

13. get_html_translation_table :-

It is used to print a translation table by using htmlspecialchars function.

<?php
print_r (get_html_translation_table()); 
?>

Output

Array ( ["] => " [&] => & [<] => < [>] => > )

14. hebrev :-

It is used to convert the herbew text into visual text.

<?php
echo hebrev("באינטרנט");//devstudioonline
?>

Output

באינטרנט

15. hebrevc :-

It is used to convert the herbew text into visual text from the new line.

<?php
echo hebrevc("ב חשון התשסג\nב חשון התשסג");
?>

Output

חשון התשסג
ב חשון התשסג

16. hex2bin :-

It decode the hexadecimal and encode in binary string.

<?php
echo hex2bin("48656c6421f72620576c6421");
?>

Output

Held!�& Wld!

17. html_entity_decode :-

It convert  all the html item into suitable character.

<?php
$string = '<a href="https://www.devstudioonline.com">devstudioonline.com</a>';
echo html_entity_decode($string);
?>

Output

devstudioonline.com

18. htmlentities :- 

It convert  all the suitable character into  html entities.

<?php
$string = '<a href="https://www.devstudioonline.com">devstudioonline.com</a>';
echo htmlentities($string);
?>

Output

<a href="https://www.devstudioonline.com">devstudioonline.com</a>

19. htmlspecialchars_decode :-

It is used to convert Html item into characters.

<?php
$string = "The quick brown fox   <b>jumps over the lazy</b> dog.";
echo htmlspecialchars_decode($string);
?>

Output

The quick brown fox jumps over the lazy dog.

20. htmlspecialchars :-

 It is used to convert special character into Html.

<?php
$string = "Students <b>welcome back</b>to the class .";
echo htmlspecialchars($string);
?>

Output

Students <b>welcome back</b>to the class .

21. implode :-

It is join the array elements and convert it in string.

<?php
$array = array('Hello','Student!','have', 'a', 'nice','day.');
echo implode(" ",$array);
?>

Output

Hello Student! have a nice day.

22. join :-

It work same as implode it is also called a implode.

<?php
$array = array('Hello','Student!','have', 'a', 'nice','day.');
echo join(" ",$array);
?>

Output

Hello Student! have a nice day.

23. lcfirst :-

It is used to convert the first character of the string in lowecase.

<?php
echo lcfirst("Hello Students Welcome Back!");
?>

Output

hello Students Welcome Back!

24. levenshtein :-

It is used to calculate the  levenshtein distance between the two string.

<?php
echo levenshtein("Hello Student this is a demo class","ello Student");
echo "<br>";
echo levenshtein("Hello Everyone","ello Everyone",12,30,34);
?>

Output

22
34

25. localeconv :-

It is used to get the information about numeric formatting.

<?php
setlocale(LC_ALL,"INDIA");
$locale_info = localeconv();
print_r($locale_info);
?>

Output

Array ( [decimal_point] => . [thousands_sep] => [int_curr_symbol] => [currency_symbol] => [mon_decimal_point] => [mon_thousands_sep] => [positive_sign] => [negative_sign] => [int_frac_digits] => 127 [frac_digits] => 127 [p_cs_precedes] => 127 [p_sep_by_space] => 127 [n_cs_precedes] => 127 [n_sep_by_space] => 127 [p_sign_posn] => 127 [n_sign_posn] => 127 [grouping] => Array ( ) [mon_grouping] => Array ( ) )

26. ltrim :-

It  remove the space or the characters from the begining of the string.

<?php
$string = "The quick brown fox jumps over the lazy dog!";
echo $string . "<br>";
echo ltrim($string ,"The quick brown fox");
?>

Output

The quick brown fox jumps over the lazy dog!
 jumps over the lazy dog!

27. md5 :-

It is used to calculate md5 hash of the string.

<?php
$string = "Example";
echo md5($string);
?>

Output

9a9fe2faa55510a85a4feb8dc776654d

28. metaphone :-

It is used to calculate the string of metaphone.

<?php
echo metaphone(" This is Example");
?>

Output

0SSKSMPL

29. money_format :-

It format the number in currency string.

<?php
$num = 678000;
setlocale(LC_MONETARY,"inr_INDIA");
echo money_format("The price is %i", $num);
?>

Output

The price is 678000.00

30. nl2br :-

It breaks the HTML line and start all newlines in a string.

<?php
echo nl2br("The quick brown fox.\njumps over the lazy dog.");
?>

Output

The quick brown fox.
jumps over the lazy dog.

31. number_format :-

It format the number in the group of thousands.

<?php
echo number_format("1000000000")."<br>";
echo number_format("450000000",2)."<br>";
echo number_format("90000000",5,",",".");// 5 add 5 zero in the last of digits.
?>

Output

1,000,000,000
450,000,000.00
90.000.000,00000

32. ord :-

It convert the first byte string value between between 0 to 255.

<?php
echo ord("D")."<br>";
echo ord("DevstudioOnline")."<br>";
?>

Output

68
68

33. parse_str :-

It parses the string into variables.

<?php
parse_str("name=John Deo&dob=12/03/1992");
echo $name."<br>";
echo $dob;
?>

Output

John Deo
12/03/1992

34. print :-

It give the output in a string format.It work same as echo both are very similar.

<?php
print "<h2>PHP is Fun!</h2>";
print "Hello Students!<br>";
print "This is a example of print function!<br>";
?>

Output

PHP is Fun!

Hello Students!
This is a example of print function!

35. printf :-

It give the ouput in proper arrange (formatted) string.

<?php
$number = 29;
$string = "India";
printf("There are %u States in %s.",$number,$string);
?>

Output

There are 29 States in India.

36. quoted_printable_decode :-

It convert the quoted printed string in 8 bit.

<?php
$string = "This is a=0Ademo.";
echo quoted_printable_decode($string);
?>

Output

This is a demo.

37. quotemeta :-

It add the backslash in front of predefined characters in a string.

<?php
$string = "Hello students. (can you hear me?)";
echo quotemeta($string);
?>

Output

Hello students\. \(can you hear me\?\)

38. rtrim :-

It remove the whitespace and or other character from the end of string.

<?php
$string = "Hello Students!";
echo $string . "<br>";
echo rtrim($string,"Students!");
?>

Output

Hello World!
Hello

39. similar_text :-

It is used to  calculate the similarity between the two strings.

<?php
echo similar_text("Hello Students","Hello Everyone this is a demo");
?>

Output

9

40. soundex :-

It is used to calculate the soundex key of a string.

<?php
$string = "Jump to the next lesson";
echo soundex($string);
?>

Output

J513

41. sprintf :-

It give the ouput in proper arrange (formatted) string by replacing the % sign by a variable.

<?php
$number = 29;
$string = "India";
$text = sprintf("There are %u States in %s.",$number,$string);
echo $text;
?>

Output

There are 29 States in India.

42. sscanf :-

It translate the string according to the formate.

<?php
$string = "age:20 height:5.8";
sscanf($string,"age:%d height:%d",$age,$height);
// show types and values
var_dump($age,$height);
?>

Output

int(20) int(5)

43. str_ireplace :-

It is used to replace the character.

<?php
echo str_ireplace("HELLO","Students","we are HELLO!");
?>

Output

we are Students!

44. str_pad :-

It pad a string from the right side upto 20 characters of length.

<?php
$string = "Hello Students";
echo str_pad($string,20,".");
?>

Output

Hello Students......

45. str_repeat :-

It is used to repeat a string.

<?php
echo str_repeat(".",18);
?>

Output

..................

46. str_replace :-

It replace all the instance of the search string with a proxy string.

<?php
echo str_replace("Sudents","John Deo","Hello Sudents!");
?>

Output

Hello John Deo!

47. str_rot13 :-

It is used to encode and decode a string by using rot13.

<?php
echo str_rot13("Good morning Students");
echo "<br>";
echo str_rot13("hanbwj jljqasv");
?>

Output

Tbbq zbeavat Fghqragf
unaojw wywdnfi

48. str_shuffle :-

It is used to jumble a string.

<?php
echo str_shuffle("This is a demo class");
?>

Output

eiisTo s asmdslach

49. str_split :-

It is used to convert a string in an array.

<?php
print_r(str_split("Australia"));
?>

Output

Array ( [0] => A [1] => u [2] => s [3] => t [4] => r [5] => a [6] => l [7] => i [8] => a )

50. str_word_count :-

It is used to count the word in a given string.

<?php
echo str_word_count("World is full of dishonest people!");
?>

Output

6

51. strcasecamp :-

It is used to campare a two case-insensitive string.

<?php
echo strcasecmp("GOOD MORNING EVERYONE!","Hi everyone!");
?>

Output

-1

52. strchr :-

It is also known as strstr() .It find the first instance of a string and return the rest of the string.

<?php
echo strchr("Keep your planet safe!","planet");
?>

Output

planet safe!

53. strcmp :- 

It is used to compare a two string which are case sensitive.

<?php
echo strcmp("clean earth will bless you with longer life!"," bless you with longer life!");
?>

Output

1

54. strcoll :-

It is used to compare a local string.

<?php
setlocale (LC_COLLATE, 'NL');
echo strcoll("Think about water conservation.”!","Hello Students!");
echo "<br>";
setlocale (LC_COLLATE, 'en_US');
echo strcoll("Hello Students!","Save water!");
?>

Output

12
-11

55. strcspn :-

It is used to print a number of character of a given string.

<?php
echo strcspn("Save Water To Secure Your Future!","r");
?>

Output

9

56. strip_tags :-

It remove(strip)  the HTML and PHP tag and form a string.

<?php
echo strip_tags("Example <i>of</i> <i>strip_tags</i>");
?>

Output

Example of strip_tags

57. stripslashes :-

It remove the backslash from the string.

<?php
echo stripslashes("I\'m selfish. ..");
?>

Output

I'm selfish. ..

58. stristr :-

It is used as a case- insensitive. It return all the string fromthe starting from the first event to the last.

// Test  the  string is found or not
<?php
  $string = 'Hello Students!';
  if(stristr($string, 'example') === FALSE) {
    echo '"This is a example of strisstr';
  }
?>

 Output

"This is a example of strisstr

59. strlen :-

It is used to find the length of the string.

<?php
$str = 'Devstudio';
echo strlen($str); // 9
//It count the space also
$str = 'on line';
echo strlen($str); // 6
?>

Output

96

60. strnatcasecmp :- 

It is used to compare the algorithm which is case insensitive  of the string in a natural order.

<?php
echo strnatcasecmp("10Life Success!","7Success Life!");
?>

Output

1

61. strnatcmp :-

It is used to compare the algorithm  of the string in a natural order.

<?php
$array1 = $array2 = array("234", "789", "456", "123");
echo "Standard string comparison\n";
usort($array1, "strcmp");
print_r($array1);
echo"<br>";
echo "\nNatural order string comparison\n";
usort($array2, "strnatcmp");
print_r($array2);
?>

Output

Standard string comparison Array ( [0] => 123 [1] => 234 [2] => 456 [3] => 789 ) 
Natural order string comparison Array ( [0] => 123 [1] => 234 [2] => 456 [3] => 789 )

62. strpbrk :-

It find the string for any set of characters.

<?php
$text = 'Hi everyone i am using Devstudioonline.';
echo strpbrk($text, 'hi');
echo strpbrk($text, 'S');
?>

Output

i everyone i am using Devstudioonline.

63. strpos :-

It find the position of first instance of a substring in a string.

<?php
echo strpos("Failure will never overtake me, if my determination to succeed is strong enough.!","strong");
?>

Output

66

64. strrchr :-

It is used to find the last instance of a character in a string.

<?php
echo strrchr("Today is Monday.","Monday");
?>

Output

Monday.

65. strrev :-

It is used to reverse a string.

<?php
echo strrev("Everyday is a new day");
?>

Output

yad wen a si yadyrevE

66. strripos :-

It is used to find the position of a last instance of a string.

<?php
echo strripos("Start a new day, with PHP!","day");
?>

Output

12

67. strrchr :-

It find the last event of character in a string.

<?php
echo strrchr("Hello Students!","Students");
?>

Output

Students!

68. strrev :-

It is used to reverse the string.

<?php
echo strrev("Hello students how are you!");
?>

Output

!uoy era woh stneduts olleH

69. strripos :-

It catch the position of the case-sensitive substring  last event in a string.

<?php
$first_name = 'John';
$last_name   = 'Doe';

$user      = strripos($first_name, $last_name);

if ($user === false) {
    echo "Hi everyone my name is ($first_name)  ($last_name)";
} else {
    echo "Congratulations!\n";
    echo " ($first_name)  ($last_name) you win the task ($user)";
}
?>

Output

Hi everyone my name is (John) (Doe)

70. strrpos :-

It catch the position of the substring  last event in a string.

<?php
echo strrpos("I like to study php, I love php !","php");
?>

Output

28

71. strstr :-

It is used to find the first instance of a string.

<?php
$email  = 'devstudioonline@.com';
$domain = strstr($email, '@');
echo $domain; 

$name = strstr($email, '@', true); 
echo $name;
?>

Output

@.comdevstudioonline

72. strspn :-

It is used to find the length first segment of the string and it contained all characters in a given mask.

<?php
echo strspn("Hello Everyone!","kHlleo");
?>

Output

5

73. strtok :-

It divide the string in small token and each token are define by any character from token. 

<?php
$token_A = strtok('/devstudioonline', '/');
$token_B = strtok('/');
var_dump($token_A, $token_B);
?>

Output

string(15) "devstudioonline" bool(false)

74. strtolower :-

 It convert the string in lowercase character.

<?php
$string = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.";
$string = strtolower($string);
echo $string;
?>

Output

the quick brown fox jumps over the lazy dog.

75. strtoupper :-

It convert the string in uppercase character.

<?php
$string = "The quick brown fox jumps over the lazy dog.";
$string = strtoupper($string);
echo $string;
?>

Output

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

76. strtr :-

It change the substring and translate the characters.

<?php
$restore = array("e" => "-", "everyone" => "using", "using" => "everyone");
echo strtr("Hi everyone i am using devstudio online", $restore);
?>

Output

Hi using i am everyone d-vstudio onlin-

77. substr-compare :-
It is used to compare two string.
<?php
echo substr_compare("Save world","Save Tree",2);
?>

Output

1

78. substr_count :-

It return the number from the substring instance.

<?php
echo substr_count("Hello Students. This is extra class","extra");
?>

Output

1

79. substr_replace :-

It replace the text from  the string.

<?php
echo substr_replace("Good","Morning",2); // 0 will start replacing the first character in the string
?>

Output

GoMorning

80. substr :-

It return the part of the string or length of the string.

<?php
echo substr("Good Evening",4);
?>

Output

Evening

81. trim :-

It remove the whitespace or other character from the begining and from the end of string.

<?php
$string = "Good Morning!";
echo $string . "<br>";
echo trim($string,"Gdg!");
?>

Output

Good Morning!
ood Mornin

82. ucfirst :-

It make the first character of the string in  uppercase.

<?php
echo ucfirst("coming Soon !");
?>

Output

Coming Soon !

83. ucwords :-
It change the first character of the string in uppercase.
<?php
echo ucwords("change the Text world");
?>

Output

Change The Text World

84. vprintf :-

It give the output in proper string format.

<?php
vprintf("%04d-%02d-%02d", explode('-', '1993-3-4')); 
?>

Output

1993-03-04

85. vsprintf :-

It return a string in a proper format.

<?php
print vsprintf("%02d-%2d-%02d", explode('-', '1993-3-4'));
?>

Output

1993- 3-04

86. wordwrap :-

By using the string break character  it cover a string with a given number of character.

<?php
$txt = "This is a example of wordwrap in php .";
$newtext = wordwrap($txt, 10, "<br />\n");
echo $newtext;
?>

Output

This is a
example of
wordwrap
in php .


Keywords: