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 to handle and change text, find and replace words, and format your output. It’s a simple and clear tutorial for anyone learning PHP.

CodeEncape - PHP String Functions – Complete Guide for Beginners (with Examples)

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.

What Are PHP String Functions?

A string in PHP is a sequence of characters. PHP provides many built-in functions to perform operations like:

  • Calculating string length
  • Changing case (upper/lower)
  • Searching text
  • Replacing text
  • Splitting strings
  • Trimming whitespace
  • Encrypting or hashing strings
  • Formatting strings
  • Converting text into arrays/li>
  • Removing unwanted characters

Let’s explore all important functions in detail with examples.

1. str_contains() – checks if one string or keyword exists inside another.

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!";
}
?>

Output

Yes, the text contains Codencape!

2. str_starts_with() – checks if a string starts with specified keyword or string.

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.";
}
?>

Output

Yes, the text contains Codencape!

3. str_ends_with() – checks if a string ends with specified keyword or string.

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";
}
?>

Output

Yes, the text contains Codencape!

4. strcmp() – compare two strings.

This function compares two strings character by character and returns :

  • `0` : if both strings are same.
  • `positive number` : if the first string is grater than second string.
  • `negative number` : if the first string is less than second string.
<?php
echo strcmp("apple", "apple");   // 0  (same)
echo strcmp("apple", "banana");  // negative value (apple < banana)
echo strcmp("banana", "apple");  // positive value (banana > apple)
?>

5. strlen() – Get String Length

strlen() returns the number of characters in a string.

<?php
$text = "Codencape Tutorials";
echo strlen($text);
?>

Output

19

6. str_word_count() – Count Words

str_word_count() returns the number of words in a string.

<?php
$text = "Learn PHP String Functions";
echo str_word_count($text); 
?>

Output

4

7. strpos() – Find Position of a Word

str_word_count() returns the number of words in a string.

<?php
echo strpos("Hello Codencape", "Codencape");
?>

Output

6

8. str_replace() – Replace Text

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");
?>

Output

Laravel Tutorials on Codencape

9. strtolower() and strtoupper()

strtolower() : converts the string to the lowercase

<?php
echo strtolower("CODENCAPE");
?>

Output

codencape

strtoupper() : converts the string to the uppercase

<?php
echo strtoupper("codencape");
?>

Output

CODENCAPE

10. ucwords(), ucfirst(), lcfirst()

ucwords() – Capitalize Every Word

<?php
echo ucwords("learn php string functions");
?>

Output

Learn Php String Functions

ucfirst() – Capitalize First Letter

<?php
echo ucfirst("Learn php string functions");
?>

Output

Learn Php String Functions

lcfirst() – Capitalize First Letter

<?php
echo lcfirst("LEARN php string functions");
?>

Output

lEARN php string functions

11. trim(), ltrim(), rtrim()

These functions remove unnecessary whitespace.

<?php
trim("   Codencape   ");
?>

Output

Codencape

12. explode() – Convert String to Array

explode() function is used to convert string into an array using separator.

<?php
$text = "php,laravel,react,node";
$array = explode(",", $text);
print_r($array);
?>

Output

Array ( [0] => php [1] => laravel [2] => react [3] => node )

13. implode() – Convert Array to String

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);
?>

Output

PHP - Laravel - React

Practical Uses of PHP String Functions

  • Checking string length
  • Remove / Trim whitespace from Start & End of the String
  • Replace the characters or string with specific character or string.
  • Convert the Text Casing ( Upper to Lower or Lower to Upper )

Summary

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.

Frequently Asked Questions (FAQ)

  1. What are string functions in PHP?

    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.

  2. Why are PHP string functions important?

    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.

  3. What is the difference between explode() and implode()?

    `explode()` converts a string into an array.

    `implode()`converts an array back into a string.

  4. How can I replace text inside a string in PHP?

    You can use `str_replace()` to replace characters or words in a string.

  5. How can i check two strings are same?

    You can use `strcmp()` to check two strings are same or not.

Categories

Latest Templates

Related Posts

Control Structures in PHP

Control structures in PHP allow developers to control the flow of code execution. In This tutorial,...

PHP Introduction

Basic introduction about what is php, benifits of php and how it runs on server and why it's widely...

Subscribe to our newsletter

Stay up-to-date about latest tech and new world. Unsubscribe at anytime!