-1

I have a string that is something like the example below,

assd *AA*the fox saw the outcome     *EAA*saassdsd  

I want to remove the whitespace between AA and EAA using regex. Using the code below I can highlight everything between AA and EAA but I only want all the spaces to be highlighted. I believe this (.*) is grouping all the characters. Can someone point me in the right direction thanks?

 \*AA\*(.*)\*EAA\*
purencool
  • 423
  • 7
  • 17

1 Answers1

1

From your question, it appears that you want to remove all spaces from given string.

function sanitize($string)
{
    $re = "/^(.*?AA)(.*)(EAA.*)$/i";

    preg_match_all($re, $string, $matches);

    if($matches) {
        $p1 = $matches[1][0];
        $p2 = $matches[2][0];
        $p3 = $matches[3][0];

        $p2 = preg_replace("/\\s+/", "", $p2);

        $result = $p1.$p2.$p3;
        return $result;
    }
    return $string;
}

$str = "assd *AA*the fox saw the outcome     *EAA*saassdsd ";
print(sanitize($str))

This will render:

assd *AA*thefoxsawtheoutcome*EAA*saassdsd 
Saleem
  • 8,728
  • 2
  • 20
  • 34