PHP String Functions (Step-by-Step Guide with Examples)

Strings are one of the most fundamental data types in PHP. PHP provides a wide range of string functions to manipulate and format text efficiently. This guide covers the most important PHP string functions with examples.

1. What are PHP String Functions?

PHP string functions allow developers to modify, search, compare, and format text dynamically. These built-in functions help in processing user inputs, handling text-based data, and generating dynamic content in PHP applications.

2. Declaring and Printing Strings in PHP

<?php
// Using double quotes
$string1 = "Hello, World!";

// Using single quotes
$string2 = 'Welcome to PHP!';

// Printing a string
echo $string1;
?>

Note: Strings in double quotes support variable interpolation, while single quotes do not.

3. Getting the Length of a String

strlen() – Get the String Length

 
<?php
$text = "Hello, PHP!";
echo strlen($text); // Output: 11
?>

4. Counting Words in a String

str_word_count() – Count Words in a String

<?php
$text = "PHP is an amazing scripting language!";
echo str_word_count($text); // Output: 6
?>

5. Reversing a String

strrev() – Reverse a String

<?php
$text = "Hello PHP";
echo strrev($text); // Output: PHP olleH
?>

6. Finding and Replacing Text

strpos() – Find Position of a Substring

<?php
$text = "Welcome to PHP programming!";
echo strpos($text, "PHP"); // Output: 11
?>

str_replace() – Replace Substring

<?php
$text = "Hello, World!";
$newText = str_replace("World", "PHP", $text);
echo $newText; // Output: Hello, PHP!
?>

Tip: str_ireplace() performs a case-insensitive replacement.

7. Changing String Case

strtoupper() – Convert to Uppercase

<?php
$text = "hello php";
echo strtoupper($text); // Output: HELLO PHP
?>

strtolower() – Convert to Lowercase

<?php
$text = "HELLO PHP";
echo strtolower($text); // Output: hello php
?>

ucfirst() – Capitalize First Letter

<?php
$text = "php is awesome!";
echo ucfirst($text); // Output: Php is awesome!
?>

ucwords() – Capitalize Each Word

<?php
$text = "php is awesome!";
echo ucwords($text); // Output: Php Is Awesome!
?>

8. Extracting Substrings

substr() – Extract Part of a String

<?php
$text = "Hello, PHP!";
echo substr($text, 7, 3); // Output: PHP
?>

Tip:

  • substr($text, 0, 5) → Extracts first 5 characters
  • substr($text, -3) → Extracts last 3 characters

9. Repeating and Padding Strings

str_repeat() – Repeat a String

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

str_pad() – Pad a String

<?php
$text = "PHP";
echo str_pad($text, 10, "*", STR_PAD_BOTH); // Output: ***PHP****
?>

10. Trimming Unwanted Characters

trim() – Remove Whitespace from Both Ends

<?php
$text = "   PHP is great!   ";
echo trim($text); // Output: PHP is great!
?>

ltrim() – Remove Leading Spaces

<?php
$text = "   PHP!";
echo ltrim($text); // Output: PHP!
?>

rtrim() – Remove Trailing Spaces

<?php
$text = "PHP!   ";
echo rtrim($text); // Output: PHP!
?>

11. Splitting and Joining Strings

explode() – Convert String to Array

 
<?php
$text = "Apple, Banana, Cherry";
$fruits = explode(", ", $text);
print_r($fruits);
?>

Tip: Useful for CSV data processing.

implode() – Convert Array to String

 
<?php
$fruits = ["Apple", "Banana", "Cherry"];
echo implode(" - ", $fruits); // Output: Apple - Banana - Cherry
?>

12. Escaping Special Characters

addslashes() – Add Slashes Before Special Characters

<?php
$text = "It's a PHP tutorial.";
echo addslashes($text); // Output: It\'s a PHP tutorial.
?>

stripslashes() – Remove Slashes

<?php
$text = "It\'s a PHP tutorial.";
echo stripslashes($text); // Output: It's a PHP tutorial.
?>

13. Hashing Strings for Security

md5() – Generate MD5 Hash

php
 
<?php
$password = "securepassword";
echo md5($password);
?>

sha1() – Generate SHA-1 Hash

<?php
$password = "securepassword";
echo sha1($password);
?>

password_hash() – Secure Hashing

<?php
$password = "securepassword";
$hash = password_hash($password, PASSWORD_BCRYPT);
echo $hash;
?>

Tip: Always use password_hash() for storing passwords securely.

Scroll to Top