PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

PHP offers a variety of built-in functions specifically designed to handle strings. Here’s a deeper look at some of the most commonly used string functions in PHP:

1. strlen()

Description: Returns the length of a given string.

				
					$string = "Hello, world!";
echo strlen($string);  // Outputs: 13

				
			

2. strpos()

Description: Finds the position of the first occurrence of a substring in a string.

				
					$myString = "Hello, world!";
$position = strpos($myString, "world");
echo $position;  // Outputs: 7

				
			

Note: Returns false if the substring is not found.

3. str_replace()

Description: Replaces all occurrences of the search string with the replacement string.

				
					$originalString = "Hello, world!";
$newString = str_replace("world", "PHP", $originalString);
echo $newString;  // Outputs: Hello, PHP!

				
			

4. substr()

Description: Returns a part of a string.

				
					$string = "Hello, world!";
$substring = substr($string, 7, 5);
echo $substring;  // Outputs: world

				
			

5. strtolower()

Description: Converts all characters in a string to lowercase.

				
					$string = "HELLO, WORLD!";
$lowercaseString = strtolower($string);
echo $lowercaseString;  // Outputs: hello, world!

				
			

6. strtoupper()

Description: Converts all characters in a string to uppercase.

				
					$string = "hello, world!";
$uppercaseString = strtoupper($string);
echo $uppercaseString;  // Outputs: HELLO, WORLD!

				
			

7. ucfirst()

Description: Capitalizes the first character of a string.

				
					$string = "hello, world!";
$capitalizedString = ucfirst($string);
echo $capitalizedString;  // Outputs: Hello, world!

				
			

8. ucwords()

Description: Capitalizes the first character of each word in a string.

				
					$string = "hello, world!";
$capitalizedWords = ucwords($string);
echo $capitalizedWords;  // Outputs: Hello, World!

				
			

9. trim()

Description: Strips whitespace (or other characters) from the beginning and end of a string.

				
					$string = "   Hello, world!   ";
$trimmedString = trim($string);
echo $trimmedString;  // Outputs: Hello, world!

				
			

10. ltrim()

Description: Strips whitespace (or other characters) from the beginning of a string.

				
					$string = "   Hello, world!";
$trimmedLeft = ltrim($string);
echo $trimmedLeft;  // Outputs: Hello, world!

				
			

11. rtrim()

Description: Strips whitespace (or other characters) from the end of a string.

				
					$string = "Hello, world!   ";
$trimmedRight = rtrim($string);
echo $trimmedRight;  // Outputs: Hello, world!

				
			

12. explode()

Description: Splits a string into an array by a specified delimiter.

				
					$string = "Hello, world!";
$array = explode(", ", $string);
print_r($array);  // Outputs: Array ( [0] => Hello [1] => world! )

				
			

13. implode()

Description: Joins array elements into a single string using a specified delimiter.

				
					$array = ["Hello", "world!"];
$string = implode(", ", $array);
echo $string;  // Outputs: Hello, world!

				
			

14. md5()

Description: Calculates the MD5 hash of a string.

				
					$string = "Hello, world!";
$hash = md5($string);
echo $hash;  // Outputs: 6cd3556deb0da54bca060b4c39479839

				
			

15. sha1()

Description: Calculates the SHA-1 hash of a string.

				
					$string = "Hello, world!";
$hash = sha1($string);
echo $hash;  // Outputs: d3486ae9136e7856bc42212385ea797094475802

				
			

16. htmlspecialchars()

Description: Converts special characters to HTML entities to prevent XSS attacks.

				
					$string = "<a href='test'>Test</a>";
$safeString = htmlspecialchars($string);
echo $safeString;  // Outputs: &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;

				
			

17. htmlentities()

Description: Converts all applicable characters to HTML entities.

				
					$string = "<a href='test'>Test</a>";
$safeString = htmlentities($string);
echo $safeString;  // Outputs: &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;

				
			

18. addslashes()

Description: Adds backslashes before certain characters in a string.

				
					$string = "Hello, 'world'!";
$safeString = addslashes($string);
echo $safeString;  // Outputs: Hello, \'world\'!

				
			

19. strstr()

Description: Finds the first occurrence of a string and returns the rest of the string.

				
					$email = "user@example.com";
$domain = strstr($email, '@');
echo $domain;  // Outputs: @example.com

				
			

20. strrev()

Description: Reverses a string.

				
					$string = "Hello, world!";
$reversedString = strrev($string);
echo $reversedString;  // Outputs: !dlrow ,olleH

				
			

21. sprintf()

Description: Returns a formatted string.

				
					$number = 5;
$location = "tree";
$formattedString = sprintf("There are %d monkeys in the %s", $number, $location);
echo $formattedString;  // Outputs: There are 5 monkeys in the tree

				
			

22. number_format()

Description: Formats a number with grouped thousands.

				
					$number = 1234.56;
$formattedNumber = number_format($number, 2, '.', ',');
echo $formattedNumber;  // Outputs: 1,234.56

				
			

23. str_pad()

Description: Pads a string to a new length.

				
					$input = "Alien";
$padded = str_pad($input, 10, "*");
echo $padded;  // Outputs: Alien*****

				
			

24. str_repeat()

Description: Repeats a string a specified number of times.

				
					$input = "Hello";
$repeated = str_repeat($input, 3);
echo $repeated;  // Outputs: HelloHelloHello

				
			

25. str_word_count()

Description: Counts the number of words in a string.

				
					$string = "Hello, world!";
$wordCount = str_word_count($string);
echo $wordCount;  // Outputs: 2

				
			

These functions are powerful tools for string manipulation and are essential for any PHP developer to master.

Scroll to Top