我想删除 php 中的标签,因为我想在将 html 解析为 DOMDocument 之前删除未使用的标签。
这是我使用的代码。光标在开始时为 0。它只是用于程序中的递归。
function delete_all_betweenV2($cursor, $beginning, $end, $string, $retainSelf) {
echo '>>>> Start '.'<br>';
$beginningPos = strpos($string, $beginning, $cursor);
$endPos = strpos($string, $end, $beginningPos);
if ($beginningPos === false || $endPos === false) {
echo '>>>> End '.'<br>';
return $string;
}
if($endPos >= strlen($string)) {
echo '>>>> End '.'<br>';
return $string;
}
$lenOfBeginning = strlen($beginning);
$lenOfEnd = strlen($end);
$result = $string;
if($retainSelf) {
echo 'b4 input String: '.$string.'<br>';
echo 'b4 cursor = : '.$cursor. '<br>';
echo 'b4 string: '.$string. '<br>';
echo 'b4 beginning Pos: '.$beginningPos.'<br>';
echo 'b4 end Pos: '.$endPos.'<br>';
echo 'b4 length to be cut is: '.(($endPos - $lenOfEnd) - $beginningPos).'<br>';
if($cursor > 0) {
echo 'cursor is greater than 0'.'<br>';
$textToDelete = substr($string, $beginningPos + $lenOfBeginning, ($endPos - $lenOfEnd) - $beginningPos);
} else {
echo 'cursor is NOT greater than 0'.'<br>';
$textToDelete = substr($string, $beginningPos + $lenOfBeginning, ($endPos - $lenOfEnd) - $beginningPos);
}
echo 'TextToDelete:'.$textToDelete.'<br>';
//$stringStart = substr($string, 0, $beginningPos + $lenOfBeginning);
//echo $stringStart.'<br>';
//$stringTail = substr($string, $endPos, strlen($string));
//echo $stringTail.'<br>';
$result = str_replace($textToDelete, '', $string);
$cursor = $beginningPos + $lenOfBeginning; // just make sure that the cursor search next character/word
echo 'After cursor = : '.$cursor. '<br>';
echo 'After result: '.$result. '<br>';
echo 'After len of result: '.strlen($result). '<br>';
} else {
//$stringStart = substr($string, 0, $beginningPos);
//echo $stringStart.'<br>';
//$stringTail = substr($string, $endPos + $lenOfEnd, strlen($string));
//echo $stringTail.'<br>';
$cursor = 0;
$textToDelete = substr($string, $beginningPos, ($endPos + $lenOfEnd) - $beginningPos);
echo 'TextToDelete:'.$textToDelete.'<br>';
$result = str_replace($textToDelete, '', $string);
}
echo '>>>> End '.'<br>';
return delete_all_betweenV2($cursor, $beginning, $end, $result, $retainSelf);
}