I have a body of text stored as a string. There are multiple substrings that i want to replace with a substring of that substring. This is a typical substring that i want to replace (note there are multiple substrings i want to replace).
$String = "loads of text [[gibberish text|Text i Want]] more text [[gibberish text|Text i Want]] more text [[if no separator then just remove tags]]";
$String = deleteStringBetweenStrings("[[", "|", $String , true);
deleteStringBetweenStrings is a recursive function that will delete all code between the 2 substrings (including the substrings) this will do what i want for the first substring but goes a bit crazy after this.
function deleteStringBetweenStrings($beginning, $end, $string, $recursive)
{
$beginningPos = strpos($string, $beginning);
$endPos = strpos($string, $end);
if ($beginningPos === false || $endPos === false)
{
return $string;
}
$textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);
$string = str_replace($textToDelete, '', $string);
if (strpos($string, $beginning) && strpos($string, $end) && $recursive == true)
{
$string = deleteStringBetweenStrings($beginning, $end, $string, $recursive);
}
return $string;
}
Is there a more efficient way for me to do this?
Expected output = "loads of text Text i Want more text Text i Want more text if no separator then just remove tags"