PHP String Functions – Complete Guide for Beginners (with Examples)
In this tutorial, you’ll learn how PHP string functions work with easy examples. We’ll show you how...
In this tutorial, you’ll learn how PHP string functions work with easy examples. We’ll show you how to handle and change text, find and replace words, and format your output. It’s a simple and clear tutorial for anyone learning PHP.
Working with strings is one of the most common tasks in PHP. Whether you are developing a login form, validating user input, building APIs, or generating dynamic text on a webpage, PHP string functions play an important role. PHP provides a rich set of built-in string functions that allow you to modify, search, replace, split, and analyze text easily.
In this tutorial, we will explore the most commonly used PHP string functions with examples.
A string in PHP is a sequence of characters. PHP provides many built-in functions to perform operations like:
Let’s explore all important functions in detail with examples.
Before PHP 8, strpos() is used to check this, but that returned a number (position of the match) or false, which often caused confusion — especially when the matching position was at 0 position (the beginning of the string). Now, str_contains() simply returns true or false.
<?php
$text = "Welcome to Codencape";
if (str_contains($text, "Codencape")) {
echo "Yes, the text contains Codencape!";
}
?>
Yes, the text contains Codencape!
This function commonly used to check URLs, filename or prefixes.
<?php
$url = "https://codencape.com";
if (str_starts_with($url, "https")) {
echo "This is a secure website.";
}
?>
Yes, the text contains Codencape!
This function commonly used to check file extensions, domain endings, URL endings, or any suffix.
<?php
if (str_ends_with("profile.jpg", ".jpg")) {
echo "This is a JPG file.";
}
if (str_ends_with("https://example.com", ".com")) {
echo "Domain ends with .com";
}
?>
Yes, the text contains Codencape!
This function compares two strings character by character and returns :
<?php
echo strcmp("apple", "apple"); // 0 (same)
echo strcmp("apple", "banana"); // negative value (apple < banana)
echo strcmp("banana", "apple"); // positive value (banana > apple)
?>
strlen() returns the number of characters in a string.
<?php
$text = "Codencape Tutorials";
echo strlen($text);
?>
19
str_word_count() returns the number of words in a string.
<?php
$text = "Learn PHP String Functions";
echo str_word_count($text);
?>
4
str_word_count() returns the number of words in a string.
<?php
echo strpos("Hello Codencape", "Codencape");
?>
6
This is one of the most frequently used string functions in PHP. It is used to replace words inside a string.
<?php
echo str_replace("PHP", "Laravel", "PHP Tutorials on Codencape");
?>
Laravel Tutorials on Codencape
strtolower() : converts the string to the lowercase
<?php
echo strtolower("CODENCAPE");
?>
codencape
strtoupper() : converts the string to the uppercase
<?php
echo strtoupper("codencape");
?>
CODENCAPE
ucwords() – Capitalize Every Word
<?php
echo ucwords("learn php string functions");
?>
Learn Php String Functions
ucfirst() – Capitalize First Letter
<?php
echo ucfirst("Learn php string functions");
?>
Learn Php String Functions
lcfirst() – Capitalize First Letter
<?php
echo lcfirst("LEARN php string functions");
?>
lEARN php string functions
These functions remove unnecessary whitespace.
<?php
trim(" Codencape ");
?>
Codencape
explode() function is used to convert string into an array using separator.
<?php
$text = "php,laravel,react,node";
$array = explode(",", $text);
print_r($array);
?>
Array ( [0] => php [1] => laravel [2] => react [3] => node )
The opposite of explode. It is used to join all elements of an array into a single string.
<?php
$arr = ["PHP", "Laravel", "React"];
echo implode(" - ", $arr);
?>
PHP - Laravel - React
PHP string functions are very powerful and more essential for any real-world project. Whether you work on a small or a large web application, you will have to use php string functions frequently. Understanding them deeply will help you write cleaner, faster, and more secure PHP code.
String functions in PHP are built-in functions, which are userd to manipulate and work with text, like searching, replacing, trimming, splitting, formatting, and comparing strings.
PHP string functions are very important for handling form data, validates user input, processing on text or string and ensuring data security. Most real-world applications use string functions regularly.
`explode()` converts a string into an array.
`implode()`converts an array back into a string.
You can use `str_replace()` to replace characters or words in a string.
You can use `strcmp()` to check two strings are same or not.
In this tutorial, you’ll learn how PHP string functions work with easy examples. We’ll show you how...
Control structures in PHP allow developers to control the flow of code execution. In This tutorial,...
Learn the basics of PHP with an introduction of variables, data types, and operators to create dynam...
Basic introduction about what is php, benifits of php and how it runs on server and why it's widely...