gpt4 book ai didi

php - 简单 : How to replace "all between" with php?

转载 作者:IT王子 更新时间:2023-10-28 23:59:34 27 4
gpt4 key购买 nike

$string = "<tag>i dont know what is here</tag>"
$string = str_replace("???", "<tag></tag>", $string);
echo $string; // <tag></tag>

那么我在寻找什么代码?

最佳答案

一个通用函数:

function replace_between($str, $needle_start, $needle_end, $replacement) {
$pos = strpos($str, $needle_start);
$start = $pos === false ? 0 : $pos + strlen($needle_start);

$pos = strpos($str, $needle_end, $start);
$end = $pos === false ? strlen($str) : $pos;

return substr_replace($str, $replacement, $start, $end - $start);
}

DEMO

关于php - 简单 : How to replace "all between" with php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6875913/

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