Skip to content
PHP Logo

What role does the strpos function play in removing content between <> tags in PHP?

The strpos function in PHP is used to find the position of the first occurrence of a substring within a string. In the context of removing content between <> tags, strpos can be used to locate the positions of the opening and closing tags, allowing us to extract the content within those tags and remove it from the original string.

php
<?php
$string = "<p>This is some <b>sample</b> text.</p>";
$start_tag = '<';
$end_tag = '>';

$start_pos = strpos($string, $start_tag);
$end_pos = strpos($string, $end_tag, $start_pos);

if ($start_pos !== false && $end_pos !== false) {
    $string = substr_replace($string, '', $start_pos, $end_pos - $start_pos + 1);
}

echo $string; // Output: "<p>This is some text.</p>"
?>