gpt4 book ai didi

PHP函数删除字符串中某些字符之间的所有字符)

转载 作者:IT王子 更新时间:2023-10-28 23:50:53 29 4
gpt4 key购买 nike

我对 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函数删除字符串中某些字符之间的所有字符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13031250/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com