0

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.

18
  • You must use preg_quote($start, '|') and preg_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. Commented Nov 16, 2018 at 16:22
  • If I do that, the result goes like this loads of text Text i Want]] more text [[if no separator then just remove tags]] Commented Nov 16, 2018 at 16:25
  • That is due to .*, you would need to use [^|]* instead. Commented Nov 16, 2018 at 16:27
  • I'm new in here, may be I need to use $String = deleteStringBetweenStrings("[[", "]]", $String); that means '[[' and ']]' as delimiter, and vertical bar would be separator. Commented Nov 16, 2018 at 16:31
  • Try $pattern = '|' . preg_quote($start, '|') . '(?:(?!' .preg_quote($start, '|') . ').)*?' . preg_quote($end, '|') . '|'; Commented Nov 16, 2018 at 16:37

1 Answer 1

0

With help of @WiktorStribiżew, I've managed it this way. it gives me result what I wanted with 1 separator, if it have more than one separator, 3rd part remain intact. It would be great if anyone help me to get ride of 3rd part. thanks every one.

function getSubstrings($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 $string
    return preg_replace($pattern, '', $string);
}

function clearString($string) {
    // to remove '[]|' these, called str_replace($shortCode, $codeReplace, $string)
    $shortCode = array( "[", "]", '|');
    $codeReplace = array('', '', '');
    // initiating $StringBetweenStrings
    $StringBetweenStrings = '';
    // creates an array of [ and |
    $dl = array("[", '|');
    // randomly, calling one of the array
    $dl_before = $dl[array_rand($dl)];

    // checking if $dl_before is '[' or '|', if it's '|' then create $dl_after ']', else that would be '|'
    $dl_after =  ($dl_before=='|') ? ']' : '|';

    // split string with preg_split
    $res_array = preg_split('~(\[\[[^][|]*\|[^][]*]])~', $string, -1, PREG_SPLIT_DELIM_CAPTURE);

    foreach ($res_array as $key => $value) {
      // calling getSubstrings to get first part or last part of substring & calling str_replace to remove delimiter and separator.
      $StringBetweenStrings .= str_replace($shortCode, $codeReplace , getSubstrings($dl_before, $dl_after, $value));
    }

// return expected string
    return $StringBetweenStrings;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.