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.

  • 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.* – Wiktor Stribiżew Nov 16 '18 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]]** – Ashley queen Nov 16 '18 at 16:25
  • That is due to `.*`, you would need to use `[^|]*` instead. – Wiktor Stribiżew Nov 16 '18 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. – Ashley queen Nov 16 '18 at 16:31
  • Try `$pattern = '|' . preg_quote($start, '|') . '(?:(?!' .preg_quote($start, '|') . ').)*?' . preg_quote($end, '|') . '|';` – Wiktor Stribiżew Nov 16 '18 at 16:37
  • **[[gibberish text|Text i Want]]** I want that, this will get **gibberish text** or **Text i Want** – Ashley queen Nov 16 '18 at 16:39
  • But you are using `preg_replace` in the code, you are *replacing* text. If you need to *extract* text, you must use `preg_match_all`, say, `preg_match_all('~\[\[([^][|]*)\|([^][]*)]]~', $s, $matches)`, see [this regex demo](https://regex101.com/r/zlx3Yp/1) – Wiktor Stribiżew Nov 16 '18 at 16:43
  • thanks for your help, but it doesn't worked as expected,may be we need to use `array_rand` or something like this. – Ashley queen Nov 16 '18 at 16:44
  • Please make sure your question reflects your *single*, *real* issue. Right now, it looks like a dupe, and I supplied the posts that help you solve those issues you have. If they are not sufficient please edit the question, describe what is not working and how uou want it to work. – Wiktor Stribiżew Nov 16 '18 at 16:46
  • I've updated original question, added more description. and could you please have a look at my expected results? hope you'll understand what I actually need. thank you. – Ashley queen Nov 16 '18 at 17:00
  • Just to make it clear: you want to generate all possible different outputs based on the `[[...|...]]` templates, right? Note that with `'~\[\[([^][|]*)\|([^][]*)]]~'`, you will get the 2 values, before `|` and after `|`. Then, after you get the values, you will be able to create your various inputs using some additional code. Just using plain regex won't yield you the expected results. – Wiktor Stribiżew Nov 16 '18 at 17:27
  • as a newbe, I'm little bit confused what you suggested, could you please update the function `deleteStringBetweenStrings` as you suggested? thank you. – Ashley queen Nov 16 '18 at 17:41
  • I've tried `$pattern = '|' . preg_quote($start) . '~\[\[([^][|]*)\|([^][]*)]]~' . preg_quote($end) . '|'; return preg_replace($pattern, '', $string);` but it creates an error _Notice: Undefined variable: pattern in_ – Ashley queen Nov 16 '18 at 17:42
  • Sure, it is a wrong pattern. Do not use `preg_replace`. You will get better luck with `preg_split` using `PREG_SPLIT_DELIM_CAPTURE` and a regex like `'~(\[\[.*?]])~'`. – Wiktor Stribiżew Nov 16 '18 at 17:43
  • Most of this clarification _should be in the text of the question_ with an [edit] so it make contextual sense. –  Nov 16 '18 at 17:47
  • See https://ideone.com/18EqTt – Wiktor Stribiżew Nov 16 '18 at 17:50
  • I've managed it this way, https://ideone.com/NLR0XQ . it gives me result what I wanted, but not sure if it's the right way. thank you @WiktorStribiżew – Ashley queen Nov 16 '18 at 20:01
  • @Ashleyqueen You may add as an answer. – Wiktor Stribiżew Nov 16 '18 at 23:23

1 Answers1

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;
}