我感兴趣的是function delete_all_between($char1, $char2, $string)
,将为$ char1和$ char2搜索$ string,如果已找到,则从这两个字符之间的子字符串中清除$ string,包括 $ char1和$ char2本身.
例:
$string = 'Some valid and <script>some invalid</script> text!';
delete_all_between('<script>', '</script>', $string);
Run Code Online (Sandbox Code Playgroud)
现在,$ string应该只包含
'Some valid and text'; //note two spaces between 'and text'
Run Code Online (Sandbox Code Playgroud)
有人有快速解决方案吗?
Tim*_*m S 51
<?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
}
Run Code Online (Sandbox Code Playgroud)
Sta*_*ers 26
这是一个oneliner:
preg_replace('/START[\s\S]+?END/', '', $string);
替换START
和END
:)积分转到另一个SO线程!
归档时间: |
|
查看次数: |
35011 次 |
最近记录: |