Lesson 20 of 50 – PHP String Functions
40%

PHP String Functions

PHP provides many built-in string functions to manipulate and process text. These functions help us find string lengths, change letter cases, replace text, search for words, and more.

1. strlen() – Find String Length

The strlen() function returns the number of bytes in a string.

<?php
$text = "Hello PHP";

echo strlen($text);
?>
Output:
9

The string contains nine ASCII characters, including the space.

Another example:

<?php
$name = "Soopro Pathshala";

echo strlen($name);
?>
Output:
16

For UTF-8 text, strlen() counts bytes, not necessarily visible characters. Use mb_strlen() when you need a character count and the required multibyte extension is available.

2. strtoupper() and strtolower()

strtoupper() converts ASCII letters to uppercase, while strtolower() converts ASCII letters to lowercase.

<?php
$text = "Hello PHP Programming";

echo strtoupper($text);
echo "<br>";
echo strtolower($text);
?>
Output:
HELLO PHP PROGRAMMING
hello php programming

Example:

<?php
$city = "Patna";

echo strtoupper($city);
?>
Output:
PATNA

These functions are useful for normalizing English text. For Unicode-aware case conversion, use appropriate multibyte string functions.

3. ucfirst(), lcfirst(), ucwords()

These functions change the case of the first character or the first character of each word.

Function Purpose
ucfirst() Capitalizes the first character of a string.
lcfirst() Converts the first character to lowercase.
ucwords() Capitalizes the first character of each word.
<?php
$text = "hello world from php";

echo ucfirst($text);
echo "<br>";
echo lcfirst("HELLO WORLD");
echo "<br>";
echo ucwords($text);
?>
Output:
Hello world from php
hELLO WORLD
Hello World From Php

These functions work with ASCII case conversion by default. They do not automatically provide complete Unicode-aware title casing.

4. trim(), ltrim(), rtrim()

These functions remove whitespace or specified characters from the beginning or end of a string.

Function Purpose
trim() Removes characters from both ends.
ltrim() Removes characters from the beginning.
rtrim() Removes characters from the end.
<?php
$text = "   Hello PHP   ";

echo trim($text);
echo "<br>";
echo ltrim($text);
echo "<br>";
echo rtrim($text);
?>

The output strings are shown without their surrounding whitespace by the browser, so use var_dump() to see the exact values.

<?php
$text = "   Hello PHP   ";

var_dump(trim($text));
var_dump(ltrim($text));
var_dump(rtrim($text));
?>
Output:
string(9) "Hello PHP"
string(12) "Hello PHP   "
string(12) "   Hello PHP"

trim() is commonly used to remove extra whitespace from user input before validation.

5. str_replace() – Replace Text

The str_replace() function replaces occurrences of specified text with new text.

<?php
$text = "I like Java. Java is popular.";

$result = str_replace("Java", "PHP", $text);

echo $result;
?>
Output:
I like PHP. PHP is popular.

Syntax:

str_replace($search, $replace, $subject);

Example: Replace a name

<?php
$message = "Welcome Rahul";

echo str_replace("Rahul", "Amit", $message);
?>
Output:
Welcome Amit

The original string is not modified automatically. The function returns the result, so assign it to a variable if you want to keep the replaced text.

6. strpos() and stripos() – Find Text

strpos() finds the position of the first occurrence of a substring. It is case-sensitive. stripos() performs a case-insensitive search for ASCII text.

<?php
$text = "Learn PHP Programming";

echo strpos($text, "PHP");
?>
Output:
6

The position starts at zero, so the letter L is at position 0 and P in PHP starts at position 6.

Case-insensitive search:

<?php
$text = "Learn PHP Programming";

echo stripos($text, "php");
?>
Output:
6

If the text is not found, these functions return false. Always use strict comparison when checking the result because position 0 is valid.

<?php
$text = "PHP is easy";

$position = strpos($text, "PHP");

if ($position !== false) {
    echo "Text found at position " . $position;
} else {
    echo "Text not found";
}
?>
Output:
Text found at position 0
7. substr() – Extract Part of a String

The substr() function returns part of a string using a starting offset and an optional length.

<?php
$text = "Hello PHP Programming";

echo substr($text, 6);
echo "<br>";
echo substr($text, 0, 5);
echo "<br>";
echo substr($text, 6, 3);
?>
Output:
PHP Programming
Hello
PHP

Syntax:

substr($string, $offset, $length);
  • $string: The original string.
  • $offset: The starting position.
  • $length: Optional number of bytes to return.

A negative offset starts counting from the end of the string.

<?php
$text = "Programming";

echo substr($text, -3);
?>
Output:
ing
8. strrev(), str_repeat(), str_word_count()

PHP provides several additional functions for common string operations.

strrev() – Reverse a string

<?php
echo strrev("HELLO");
?>
Output:
OLLEH

str_repeat() – Repeat a string

<?php
echo str_repeat("PHP ", 3);
?>
Output:
PHP PHP PHP 

str_word_count() – Count words

<?php
$text = "PHP is easy to learn";

echo str_word_count($text);
?>
Output:
5

str_word_count() follows its own rules for identifying words and is not a complete language-aware word counter.

9. Practical Example: Format a Student Name

The following example removes surrounding whitespace, converts the name to lowercase, and capitalizes each word for display.

<?php
$name = "   rahul kumar   ";

$name = trim($name);
$name = strtolower($name);
$name = ucwords($name);

echo "Student Name: " . $name;
?>
Output:
Student Name: Rahul Kumar

These functions are useful for basic text formatting. For real user accounts, do not assume that changing capitalization is appropriate for every person's name.

Practice Assignments
  1. Find the length of the string "Computer Classes".
  2. Convert "hello world" to uppercase and lowercase.
  3. Capitalize the first letter of "welcome to php".
  4. Remove surrounding spaces from a string using trim().
  5. Replace "JavaScript" with "PHP" in a sentence.
  6. Find the position of "PHP" inside "I am learning PHP".
  7. Extract the word "Programming" from "PHP Programming".
  8. Reverse the word "COMPUTER".
  9. Count the words in "I am learning PHP programming".

🧠 Quick Quiz

1. Which function returns the byte length of a string?




2. Which function replaces text in a string?




3. What does strpos() return when it finds a substring at the beginning?