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.
Index
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.
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.
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?"
?> This function converts the string from ASCII characters into equivalent hexadecimal values.
Example
<?php
$m = bin2hex("Errorsea");
echo($m); //result: 4572726f72736561
?> 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"
?> 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"
?> 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,"
?> This function is used for converting a string from one Cyrillic character set into another.
Such as:
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.
This function decodes the uuencoded strings.
Example
<?php
$m = "(17)R;W)S96$` `" ;
echo convert_uudecode($m); // result: "Errorsea"
?> This function encodes the character strings.
Example
<?php
$m = "Errorsea" ;
echo convert_uuencode($m); // result: "(17)R;W)S96$` `"
?> 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?
This function is used for counting cyclic redundancy of a string.
Example
<?php
$m = crc32("Errorsea");
printf("%u\n",$m); // result: 2543775366
?> This function outputs one or more strings.
Example
<?php
echo "Errorsea"; // result: "Errorsea"
?> 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." )
?> 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()
This function converts the string of hexadecimal values into equivalent ASCII characters.
Example
<?php
$m = hex2bin("4572726f72736561");
echo($m); //result: "Errorsea"
?> 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."
?> This function converts the first character of string into lower case.
Example
<?php
echo lsfirst("Errorsea"); // result: "errorsea"
?> 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."
?> 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 "
?> This function converts the string into equivalent md5 hash format.
Example
<?php
$m = "Errorsea";
echo md5($m); // result: f896a897d60fb4f5bbb641437fa995b1
?> This function counts the metaphone keyword for the string.
Example
<?php
echo metaphone("Errorsea"); // result: ERRS
?> This function puts <br> tag in place of \n in the string.
Example
<?php
echo nl2br("Errorsea.\nBest");
?> 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
?> 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
?> This function outputs one or more strings.
Example
<?php
print "Errorsea"; // result: "Errorsea"
?> This function outputs the formatted one or more strings.
Example
<?php
$no = 1;
printf("Errorsea is %u website",$no);
// result: "Errorsea is 1 website"
?> This function converts the string into equivalent sha1 hash formate.
Example
<?php
$m = "Errorsea";
echo sha1($m);
// result:41b12b63eed7ebd228932d59f79ee7677bc2e80e
?> This function is used for finding similarities between two strings.
Example
<?php
echo similar_text("Hello Yash","Hello Nick"); // result: 6
?> This function finds the soundex key for a string.
Example
<?php
$m = "Errorsea";
echo soundex($m); // result: E662
?> This function replaces some character of one string with other character string.
Example
<?php
echo str_ireplace("Yash","Nick","Hello Yash ! ");
// result: "Hello Nick ! "
?> This function expands the string to a new length.
Example
<?php
$m = "Errorsea";
echo str_pad($m,10,"!"); // result: "Errorsea!!"
?> This function repeats the string to a specific number of count.
Example
<?php
$m = "Errorsea";
echo str_pad($m,3); // result: "ErrorseaErrorseaErrorsea"
?> This function replaces some character of one string with other character string.
Example
<?php
echo str_replace("Yash","Nick","Hello Yash ! ");
// result: "Hello Nick ! "
?> This function converts the string into equivalent rot13 encoded format.
Example
<?php
$m = "Errorsea";
echo str_rot13($m); // result: Reebefrn
?> This function used for randomly shuffling the characters of the string.
Example
<?php
echo str_shuffle("Errorsea"); // result: aEeorrrs
?> 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 )
?> This function is used for counting the number of words in the string.
Example
<?php
echo str_word_count("Errorsea is love"); // result: 3
?> 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.
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"
?> 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.
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
?> This function returns the length of the string.
Example
<?php
echo strlen("Errorsea"); // result: 8
?> This function reverses the string.
Example
<?php
echo strrev("Errorsea"); // result: aesrorrE
?> 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"
?> This function converts the string to lower case.
Example
<?php
echo strtolower("Errorsea"); // result: "errorsea"
?> This function converts the string to upper case.
Example
<?php
echo strtoupper("Errorsea"); // result: "ERRORSEA"
?> This function returns a substring of a particular string.
Example
<?php
echo substr("Errorsea ",5); // result: "sea"
?> 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
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.
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…