PHP String Functions - Codingque

PHP String Functions

Learn about commonly used string functions in PHP with examples and explanations.

Introduction to PHP String Functions

PHP provides numerous built-in functions to manipulate and work with strings. These functions make it easier to handle and transform string data.

1. strlen() - String Length

The strlen() function returns the length of a string.

<?php
$str = "Hello, World!";
$length = strlen($str);
echo $length; // Outputs: 13
?>

Explanation: The strlen() function calculates the total number of characters in the string $str, including spaces and punctuation.

2. str_replace() - Replace Substring

The str_replace() function replaces all occurrences of a search string with a replacement string.

<?php
$str = "Hello, World!";
$newStr = str_replace("World", "PHP", $str);
echo $newStr; // Outputs: Hello, PHP!
?>

Explanation: The str_replace() function searches for "World" in the string $str and replaces it with "PHP".

3. strtolower() and strtoupper() - Convert Case

The strtolower() function converts a string to lowercase, while the strtoupper() function converts it to uppercase.

<?php
$str = "Hello, World!";
$lowerStr = strtolower($str);
$upperStr = strtoupper($str);

echo $lowerStr; // Outputs: hello, world!
echo $upperStr; // Outputs: HELLO, WORLD!
?>

Explanation: The strtolower() function converts all characters in the string to lowercase, and strtoupper() converts them to uppercase.

4. substr() - Extract Substring

The substr() function returns a part of a string starting from a specified position.

<?php
$str = "Hello, World!";
$subStr = substr($str, 7, 5);
echo $subStr; // Outputs: World
?>

Explanation: The substr() function extracts a substring starting from position 7 (zero-based index) with a length of 5 characters.

5. strpos() - Find Position of Substring

The strpos() function returns the position of the first occurrence of a substring in a string.

<?php
$str = "Hello, World!";
$position = strpos($str, "World");

echo $position; // Outputs: 7
?>

Explanation: The strpos() function returns the position of the first occurrence of "World" in the string $str, which is at position 7.

6. trim() - Remove Whitespace

The trim() function removes whitespace from both ends of a string.

<?php
$str = "  Hello, World!  ";
$trimmedStr = trim($str);
echo $trimmedStr; // Outputs: Hello, World!
?>

Explanation: The trim() function removes spaces from the beginning and end of the string $str, leaving the inner content unchanged.

7. str_repeat() - Repeat a String

The str_repeat() function repeats a string a specified number of times.

<?php
$str = "PHP ";
$repeatedStr = str_repeat($str, 3);
echo $repeatedStr; // Outputs: PHP PHP PHP 
?>

Explanation: The str_repeat() function repeats the string "PHP " three times, resulting in "PHP PHP PHP ".

8. strrev() - Reverse a String

The strrev() function reverses a string.

<?php
$str = "Hello, World!";
$reversedStr = strrev($str);
echo $reversedStr; // Outputs: !dlroW ,olleH
?>

Explanation: The strrev() function reverses the string "Hello, World!" to "!dlroW ,olleH".

Previous Next
Modern Footer