Remove a Substring Based on Begining and Ending Tags in PHP

If you want to remove a substring from a PHP string based on specific beginning and ending tags, you can use the strstr and str_replace functions. Here’s an example:

removeSubstringBetweenTags($inputString, $startTag, $endTag) 
    $startPosition = strpos($inputString, $startTag);
    $endPosition = strpos($inputString, $endTag);

    if ($startPosition !== false && $endPosition !== false) 
        $substringToRemove = substr($inputString, $startPosition, $endPosition - $startPosition + strlen($endTag));
        $outputString = str_replace($substringToRemove, '', $inputString);
        return $outputString;

    return $inputString; // Return the original string if tags are not found

// Example usage:
$inputString = "This is a <span>sample</span> string with <div>tags</div> to remove.";
$startTag = "<span>";
$endTag = "</span>";

$outputString = removeSubstringBetweenTags($inputString, $startTag, $endTag);

echo $outputString;

This example defines a function removeSubstringBetweenTags that takes the input string and the starting and ending tags. It finds the positions of these tags using strpos and then extracts the substring to be removed using substr. Finally, it uses str_replace to replace this substring with an empty string, effectively removing it.

Note: This example assumes that there is only one occurrence of the specified start and end tags. If there are multiple occurrences, you may need to modify the function to handle them accordingly.