我对此感兴趣function delete_all_between($char1, $char2, $string) ,它将在给定的$ string中搜索$ char1和$ char2,如果找到了该字符串,则从这两个字符之间的子字符串中清除$ string, 包括 $ char1和$ char2本身。
function delete_all_between($char1, $char2, $string)
例:
$string = 'Some valid and <script>some invalid</script> text!'; delete_all_between('<script>', '</script>', $string);
现在,$ string应该只包含
'Some valid and text'; //note two spaces between 'and text'
有人有快速解决方案吗?
<?php $string = 'Some valid and <script>some invalid</script> text!'; $out = delete_all_between('<script>', '</script>', $string); print($out); function delete_all_between($beginning, $end, $string) { $beginningPos = strpos($string, $beginning); $endPos = strpos($string, $end); if ($beginningPos === false || $endPos === false) { return $string; } $textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos); return delete_all_between($beginning, $end, str_replace($textToDelete, '', $string)); // recursion to ensure all occurrences are replaced }