PHP Strings
PHP Strings
Section titled “PHP Strings”A string is a sequence of characters used to represent text. In PHP, strings can be created using single quotes ('...'), double quotes ("..."), or heredoc/nowdoc syntax.
Double quotes ("") and heredoc allow variable interpolation and processing of escape sequences (like \n for newline), while single quotes ('') and nowdoc treat the content almost literally.
Common String Functions
Section titled “Common String Functions”PHP offers a vast library of functions for string manipulation. Here are some of the most frequently used ones:
Get The Length of a String
Section titled “Get The Length of a String”The strlen() function returns the length of a string in bytes (not necessarily characters for multi-byte encodings like UTF-8).
For accurate character counting with multi-byte encodings (like UTF-8), use mb_strlen().
<?php$string = "Hello world!";echo strlen($string); // Outputs: 12
$utf8String = "你好世界"; // UTF-8 encoded stringecho strlen($utf8String); // Outputs: 12 (bytes)echo mb_strlen($utf8String, 'UTF-8'); // Outputs: 4 (characters)?>Count The Number of Words in a String
Section titled “Count The Number of Words in a String”The str_word_count() function counts the number of words in a string.
<?php$sentence = "This is a simple sentence.";echo str_word_count($sentence); // Outputs: 5?>Reverse a String
Section titled “Reverse a String”The strrev() function reverses a string. Note: This function is byte-based and may not work correctly with multi-byte character encodings.
<?php$text = "Hello world!";echo strrev($text); // Outputs: !dlrow olleH?>Search For Text Within a String
Section titled “Search For Text Within a String”The strpos() function finds the numeric position of the first occurrence of a substring within a string (case-sensitive). It returns the starting position index (0-based) or false if the substring is not found.
Important: Always use the strict comparison operator (=== or !==) when checking the return value of strpos(), because the position 0 evaluates to false in loose comparison (==).
PHP 8 introduced str_contains(), which returns a boolean (true/false) and is often more readable for simply checking existence.
<?php$haystack = "Hello world!";$needle = "world";
$position = strpos($haystack, $needle);
if ($position !== false) { echo "Found '$needle' at position: $position"; // Outputs: Found 'world' at position: 6} else { echo "'$needle' not found.";}
// Using str_contains() (PHP 8+)if (str_contains($haystack, $needle)) { echo "\nThe haystack contains the needle."; // Outputs: The haystack contains the needle.}?>Other useful search functions include stripos() (case-insensitive strpos), strrpos() (find last occurrence), strripos() (case-insensitive last occurrence), strstr() (find first occurrence and return rest of string), str_starts_with() (PHP 8+), and str_ends_with() (PHP 8+).
Replace Text Within a String
Section titled “Replace Text Within a String”The str_replace() function replaces all occurrences of a search string with a replacement string (case-sensitive).
<?php$original = "Hello world!";$updated = str_replace("world", "PHP", $original);echo $updated; // Outputs: Hello PHP!?>For case-insensitive replacement, use str_ireplace().
Working with Multi-byte Strings (UTF-8)
Section titled “Working with Multi-byte Strings (UTF-8)”Standard PHP string functions operate on bytes. If you are working with multi-byte character encodings like UTF-8 (which is highly recommended for web applications), you should use the corresponding mb_* functions from the Multibyte String extension (often enabled by default).
Examples: mb_strlen(), mb_strpos(), mb_substr(), mb_strtolower(), mb_strtoupper().
<?php$utf8String = "你好世界";
// Correct lengthecho mb_strlen($utf8String, 'UTF-8'); // Outputs: 4
// Correct substringecho mb_substr($utf8String, 0, 2, 'UTF-8'); // Outputs: 你好?>Complete PHP String Reference
Section titled “Complete PHP String Reference”This was just a small selection. PHP provides many more functions for tasks like converting case, trimming whitespace, formatting, splitting, joining strings, and more.
For a complete reference of all string functions, including descriptions and examples, consult the official PHP documentation available on PHP.net.