What function can be used to search for a specific substring within a string in PHP?
To search for a specific substring within a string in PHP, you can use the `strpos()` function. This function returns the position of the first occurrence of a substring within a string. If the substring is not found, it returns `false`. You can then use this position to extract or manipulate the substring as needed.
$string = "Hello, world!";
$substring = "world";
if(strpos($string, $substring) !== false){
echo "Substring found at position: " . strpos($string, $substring);
} else {
echo "Substring not found";
}
Keywords
Related Questions
- How can server settings impact the functionality of PHP sessions in a web application?
- How can the readability and maintainability of PHP code be improved by proper indentation and formatting?
- What is the significance of the "#xy$#S" pattern in the preg_match function in PHP, and how does it affect the matching process?