Simple: How to replace "all between" with php? [duplicate] Simple: How to replace "all between" with php? [duplicate] php php

Simple: How to replace "all between" with php? [duplicate]


A generic function:

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


$search = "/[^<tag>](.*)[^<\/tag>]/";$replace = "your new inner text";$string = "<tag>i dont know what is here</tag>";echo preg_replace($search,$replace,$string);

outputs:

<tag>your new inner text</tag>


$string = "<tag>I do not know what is here</tag>";$new_text = 'I know now'; echo preg_replace('#(<tag.*?>).*?(</tag>)#', '$1'.$new_text.'$2' , $string); //<tag>I know now</tag>