PHP函数删除字符串中某些字符之间的所有字符
我对此感兴趣function delete_all_between($char1, $char2, $string)
,它将在给定的$
string中搜索$ char1和$ char2,如果找到了该字符串,则从这两个字符之间的子字符串中清除$ string, $ char1和$
char2本身。
例:
$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
}
以上是 PHP函数删除字符串中某些字符之间的所有字符 的全部内容, 来源链接: utcz.com/qa/414218.html