PHP String Functions

PHP string functions are an important part of PHP core development. These are pre-made string functions that you can use directly without any type of installation library.

Related: 12 best examples of PHP echo and print

We have some of the most commonly used PHP string functions available, which you can easily teach to use.

PHP String Functions

No.FunctionDescription
1str_repeatRepeats a string
2str_replaceReplace characters into a string
3strlenCalculate the length of a string
4PHP echoThis is used to display the output of the string
5implode Converts a string from the array
6explodeUsed to break a string into the array
7printUsed to display the output one or more strings
8str_splitBreak a string into an array format
9fprintfUsed to write a formatted string
10strrevReverses the string
11strtolowerIt converts a string to lowercase letters
12htmlentitiesThis is used to switch the characters to HTML entities (code)
13trimRemoves whitespace from the string
14ucfirstTransforms the first character of the string to uppercase
15md5This converts a normal string into a secure manner
16substrReturns the part of a string according to the numbers
17substr_countCounts the substring as many times as it is in the string
18money_formatThis is used to make a string as a currency string
19number_formatGives the formats to the number (comma)
20strtoupperIt converts a string to uppercase letters
21str_word_countCalculate the number of words inside the string
22strcasecmpUsed to compare two strings

PHP String Functions With Examples

80 out of 100 developers require string functions. Because when you work on a large web application, then there is a lot of work on the PHP function.

Related: Best examples of learning PHP switch case

We have some great examples of PHP string functions to explain you more thoroughly, after seeing which you will know how to use string functions.

Str_repeat()

This function is used to repeat any string. In this, you have to enter a value of how many times you want to repeat your string.

<?php
	echo str_repeat("Repeat, ",5);
?>

Output: - Repeat, Repeat, Repeat, Repeat, Repeat,

Strlen()

The strlen function is used to get the length of any string.

<?php
	echo strlen("This is my string");
?>

Output: - 17

Str_split()

The Str_split function is used to convert a string to the array.

<?php
	echo "<pre>";
	print_r(str_split("my string"));
?>

Output: - Array
(
    [0] => m
    [1] => y
    [2] =>  
    [3] => s
    [4] => t
    [5] => r
    [6] => i
    [7] => n
    [8] => g
)

Implode()

The implode function is used to convert an array into a string.

<?php
	$myarry = array('This','is','my','arry');
	echo implode(" ",$myarry);
?>

Output: - This is my arry

Strrev()

The strrev function is used to reverse a string.

<?php
	echo strrev("My string");
?>

Output: - gnirts yM

Explode()

The explode function is used to break a string into the array.

<?php
	$string = "This is my string";
	echo "<pre>";
	print_r (explode(" ",$string));
?>

Output:- Array
(
    [0] => This
    [1] => is
    [2] => my
    [3] => string
)

Ucfirst()

This function is used to transform the first character of the string to uppercase.

<?php
	echo ucfirst("my string");
?>

Output:- My string

md5()

md5 is such a function that converts a normal string into a secure mode. This function is often used to keep the password secure.

<?php
	$password = "admin";
	echo md5($password);
?>  

Output:- 21232f297a57a5a743894a0e4a801fc3

Substr()

This function returns the part of a string according to the numbers.

<?php
	echo substr("This is my string",10);
?>

Output:- string

Substr_count()

This function counts the substring as many times as it is in the string.

<?php
	echo substr_count("my string, i have string, i had string","string");
?>

Output:- 3

Number_format()

It gives the formats to the number (Commas). When the value of numbers increases too much, it counts and adds a comma to that value.

<?php
	echo number_format("200000000");
?>

Output:- 200,000,000

Strtolower()

It converts a string to lowercase letters.

<?php
	echo strtolower("MY STRING");
?>

Output:- my string

Strtoupper()

It converts a string to uppercase letters.

<?php
	echo strtoupper("my string");
?>

Output:- MY STRING

Str_word_count()

This function calculates the number of words inside the string.

<?php
	echo str_word_count("This is my new string");
?>

Output:- 5

Trim()

This function removes whitespace from the string.

<?php
	$string = "This	is my	string	";
	echo ltrim($string);
?>

Output:- This is my string

Related: 7 easiest example of PHP explode and PHP implode