Some Handy php string function

Published on : April 27, 2026

Author:

Category: Uncategorized


str_split()

str_split() works like explode() in some manner, it is used for splitting a string into an array.

Syntax:

str_split($mystring, length);

Example:

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

 
Output:

Array
(
[0] => W
[1] => h
[2] => a
[3] => T
)

str_replace()

If you want to replace some specified characters with other characters in PHP, str_replace() is the best choice. It has many rules like:

  1. If we want to search a string in an array, it returns as an array.
  2. Find and Replace is performed with each array element as we want to search a string in an array.
  3. If we are finding an array and also replacing with it with an array, an empty string will be used as replace.
  4. In case, replace is a string and find is an array, then it uses replace sting as find value.
Syntax:
str_replace(find, replace, string, count);

Example:
<?php
echo str_replace("How", "Dear", "How Di");
?>

Output:

How Dear

md5()

md5() function calculates Message-Digest Algorithm (md5) hash of a string. It has two parameters, first one is required and second one is optional which was included in PHP 5.0.

Syntax:

md5($str, raw);

Example:

<?php
$mystr = "How Are You";
echo md5($mystr);
?>

Output:

9e227bb366c119c7f27a7115f0136f42

implode()

implode() is a reverse of explode(), means it joins an array into a sring. It has two parameters, first one is optional and second one is required.

Syntax:

imlode(string_separator, array);

Example:

<?php
$arr = array("Hello", "Dear", "How", "Are", "You");
echo implode(" ", $arr);
?>

Output:

Hello Dear How Are You

explode()

explode() explodes or break a string into an array. We can easily understand the functionality of this function by the name “explode”. explode() has three parameters, first 2 are required and third is optional.

Syntax:

explode(string_separator, $str, limit);

Example:

<?php
$mystr = "Hello Dear, How are you";
print_r(explode(" ", $mystr));
?>

Output:

Array ( [0] => Hello [1] => Dear, [2] => How [3] => are [4] => you )

echo()

This function helps to output more than one strings. Parameters in this function are optional.

count_chars()

This string function returns how many times a character (ASCII) appears in a string depending on the modes we specified.

By default, mode is 0 that returns an array with Key (ASCII Value) and Value (Number of Occurrences.

Mode 1 returns an array with same functionality as Mode 2, but it only lists appearance of characters which are greater than zero.

Mode 2 returns occurrence which are equal to zero.

Mode 3 returns string with difference characters used.

Mode 4 generates string of all unused characters.

Syntax:

count_chars($str, mode);

Example:

<?php
$mystr = "Hello Dear, How are you";
print_r(count_chars($mystr, 0));
print_r(count_chars($mystr, 1));
print_r(count_chars($mystr, 2));
echo count_chars($mystr, 3);
echo count_chars($mystr, 4);
?>


Leave a Reply

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