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.
The strlen() function returns the number
of bytes in a string.
<?php
$text = "Hello PHP";
echo strlen($text);
?>
9
The string contains nine ASCII characters, including the space.
Another example:
<?php
$name = "Soopro Pathshala";
echo strlen($name);
?>
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.
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);
?>
HELLO PHP PROGRAMMING
hello php programming
Example:
<?php
$city = "Patna";
echo strtoupper($city);
?>
PATNA
These functions are useful for normalizing English text. For Unicode-aware case conversion, use appropriate multibyte string functions.
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);
?>
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.
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));
?>
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.
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;
?>
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);
?>
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.
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");
?>
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");
?>
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";
}
?>
Text found at position 0
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);
?>
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);
?>
ing
PHP provides several additional functions for common string operations.
strrev() – Reverse a string
<?php
echo strrev("HELLO");
?>
OLLEH
str_repeat() – Repeat a string
<?php
echo str_repeat("PHP ", 3);
?>
PHP PHP PHP
str_word_count() – Count words
<?php
$text = "PHP is easy to learn";
echo str_word_count($text);
?>
5
str_word_count() follows its own rules
for identifying words and is not a complete
language-aware word counter.
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;
?>
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.
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?