I found this on SO, Dynamically Replace Substring With Substring Using PHP
I need something similar to this, don't want to remove first part but randomly both.
that means, I need sometimes it would get first part of the substring, and sometimes last part.
function deleteStringBetweenStrings($start, $end, $string) {
// create a pattern from the input and make it safe to use in a regular expression
$pattern = '|' . preg_quote($start) . '(.*)' . preg_quote($end) . '|U';
// replace every occurrence of this pattern with an empty string in full $s
tring
return preg_replace($pattern, '', $string);
}
$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);
echo $String;
//loads of text more text more text
If I use this function with '[[' and ']]' delimiters, this removes full substring inside the delimiters. but I need that string should contain any one part (random) of the substring inside the delimiters. My expected result would be like-
result #1 = loads of text gibberish text more text Text i Want more text if no separator then just remove tags
result #2 = loads of text Text i Want more text Text i Want more text if no separator then just remove tags
result #3 = loads of text Text i Want more text gibberish text more text if no separator then just remove tags
any assistance would be greatly appreciated.
preg_quote($start, '|')andpreg_quote($end, '|'). This is because you chose|as a delimiter. As in the accepted answer: If the optional delimiter is specified, it will also be escaped. This is useful for escaping the delimiter that is required by the PCRE functions. The / is the most commonly used delimiter..*, you would need to use[^|]*instead.$String = deleteStringBetweenStrings("[[", "]]", $String);that means '[[' and ']]' as delimiter, and vertical bar would be separator.$pattern = '|' . preg_quote($start, '|') . '(?:(?!' .preg_quote($start, '|') . ').)*?' . preg_quote($end, '|') . '|';