What are the best practices for combining substr() with strpos() in PHP?

When combining substr() with strpos() in PHP, it's important to first check if the substring exists within the main string using strpos(). If the substring is found, then you can use substr() to extract the desired portion of the main string based on the position returned by strpos(). This ensures that you are working with valid positions and prevents errors.

$mainString = "Hello, World!";
$substring = "World";

if (($pos = strpos($mainString, $substring)) !== false) {
    $extractedString = substr($mainString, $pos);
    echo $extractedString; // Output: World!
}