-3

I'm triying to delete the text between include and the next & in the text:

filter[mat]=1&filter[status]=available&include=homeCompetitor,awayCompetitor&sort=scheduled

and get a text like:

filter[mat]=1&filter[status]=available&sort=scheduled

I tried with the following codes:

function delete_all_between($string)
{
    $beginningPos = strpos($string, $beginning);
    $endPos = strpos($string, $end);
    if ($beginningPos === false || $endPos === false)
        return $string;
}

and also with:

preg_replace("/\include[^]+\&/","",$string);

But none worked.

It may also be that the text also has the following format:

filter[mat]=1&filter[status]=available&sort=scheduled&include=homeCompetitor,awayCompetitor

With which I need to delete from the word include to the end (in this case there is no &)

EDIT: This question isn't duplicated because that answer didn't work for me!

Nebur81
  • 29
  • 1
  • 9
  • 3
    Why not split it on `&` characters with explode? This also looks like a URL parameter string, so you can use `parse_str()` to turn it into an associative array. – Barmar Aug 28 '19 at 16:44
  • 1
    Why do you escape the `i` character in the regexp? `\i` means the TAB character. – Barmar Aug 28 '19 at 16:45
  • What's the purpose of `[^)]+`? There are no `)` characters in your string, why do you have to exclude them? – Barmar Aug 28 '19 at 16:46
  • It's usually best to put a regexp in single quotes, so that escape sequences aren't processed by PHP, only the regexp engine. – Barmar Aug 28 '19 at 16:46
  • https://regex101.com/r/3zUwJF/1, something like this should help you – Dev Man Aug 28 '19 at 16:49
  • [^)]+ was a mistake, I edited. That works for the case: `filter[mat]=1&filter[status]=available&include=homeCompetitor,awayCompetitor&sort=scheduled` but not when the include is in the last part of url – Nebur81 Aug 28 '19 at 16:49

2 Answers2

2

The string you are dealing with is in a particular format that can be parsed by parse_str and reassembled via http_build_query.

<?php

$str = "filter[mat]=1&filter[status]=available&include=homeCompetitor,awayCompetitor&sort=scheduled";
parse_str($str, $output);
unset($output["include"]);
echo http_build_query($output);
Alex Barker
  • 4,316
  • 4
  • 28
  • 47
-2

Try this :

preg_replace('/(include(.*)\&)|(\&include([^&]*)$)/i', '', $string)

I tested with :

  • filter[mat]=1&filter[status]=available&include=homeCompetitor,awayCompetitor&sort=scheduled

Result : filter[mat]=1&filter[status]=available&sort=scheduled

  • filter[mat]=1&filter[status]=available&include=homeCompetitor,awayCompetitor

Result : filter[mat]=1&filter[status]=available

  • filter[mat]=1&filter[status]=available&INCLUDE=homeCompetitor,awayCompetitor&sort=scheduled

Result : filter[mat]=1&filter[status]=available&sort=scheduled