PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
To find a substring within a string in PHP, you can use several built-in functions. Here are a few of the most commonly used ones:
false
if the substring is not found.$str = "Hello, world!"; $pos = strpos($str, "world"); if ($pos !== false) { echo "The string 'world' was found at position $pos"; } else { echo "The string 'world' was not found"; }
Note that strpos()
is case-sensitive. If you want a case-insensitive search, you can use stripos()
instead.
false
if the substring is not found.$str = "Hello, world!"; $sub = strstr($str, "world"); if ($sub !== false) { echo "The substring 'world' was found and the rest of the string is: $sub"; } else { echo "The string 'world' was not found"; }
Again, strstr()
is case-sensitive. If you want a case-insensitive search, you can use stristr()
instead.
$str = "Hello, world! The world is round."; $count = substr_count($str, "world"); echo "The string 'world' was found $count times";
Remember that string positions in PHP start from 0
, not 1
. So if strpos()
returns 0
, it means the substring was found at the beginning of the string. That's why when checking the return value of strpos()
or strstr()
, you should use !== false
rather than != false
, because 0
is considered false
when using !=
, but not when using !==
.
Query substring presence in PHP:
strpos()
and strstr()
can be used.// File: substring_presence_query.php $haystack = "Searching for substring in PHP"; $needle = "substring"; if (strpos($haystack, $needle) !== false) { echo "Substring found!"; } else { echo "Substring not found."; } // Output: Substring found!
Check if a string contains a substring in PHP:
strpos()
function can be employed to check if a string contains a particular substring.// File: check_substring_presence_strpos.php $haystack = "Check for substring in PHP"; $needle = "substring"; if (strpos($haystack, $needle) !== false) { echo "Substring found!"; } else { echo "Substring not found."; } // Output: Substring found!
PHP strpos()
function for substring search:
strpos()
function returns the position of the first occurrence of a substring in a string.// File: strpos_substring_search.php $haystack = "PHP strpos function example"; $needle = "strpos"; $position = strpos($haystack, $needle); if ($position !== false) { echo "Substring found at position: $position"; } else { echo "Substring not found."; } // Output: Substring found at position: 4
Case-insensitive substring search in PHP:
stripos()
.// File: case_insensitive_strpos.php $haystack = "Case-Insensitive Substring Search"; $needle = "search"; if (stripos($haystack, $needle) !== false) { echo "Substring found!"; } else { echo "Substring not found."; } // Output: Substring found!
Determine position of substring in PHP:
strpos()
to find the position of the first occurrence of a substring.// File: determine_position_strpos.php $haystack = "Position of Substring in PHP"; $needle = "Substring"; $position = strpos($haystack, $needle); if ($position !== false) { echo "Substring found at position: $position"; } else { echo "Substring not found."; } // Output: Substring found at position: 12
PHP strstr()
for substring query:
strstr()
function returns the part of the string from the first occurrence of the substring.// File: strstr_substring_query.php $haystack = "Using strstr() in PHP"; $needle = "strstr"; $result = strstr($haystack, $needle); if ($result !== false) { echo "Substring found: $result"; } else { echo "Substring not found."; } // Output: Substring found: strstr() in PHP
Substring existence check in PHP:
strpos()
.// File: substring_existence_check.php $haystack = "Existence of Substring in PHP"; $needle = "Substring"; if (strpos($haystack, $needle) !== false) { echo "Substring found!"; } else { echo "Substring not found."; } // Output: Substring found!
Handling multiple occurrences of a substring in PHP:
strpos()
.// File: multiple_occurrences_handling.php $haystack = "PHP has multiple substring occurrences in PHP"; $needle = "PHP"; $position = 0; while (($position = strpos($haystack, $needle, $position)) !== false) { echo "Substring found at position: $position\n"; $position += strlen($needle); } // Output: // Substring found at position: 0 // Substring found at position: 25 // Substring found at position: 46