Regex – how to match until something

How do you replace an occurence until a certain point: You can see my
example code below and the output:

$string1 = ‘Hello world today’;
$string2 = ‘Hello blahblah’;

$string1 = preg_replace ( ‘/Hello.*(?!today)/’, ‘Hello ***’ , $string1
);
$string2 = preg_replace ( ‘/Hello.*(?!today)/’, ‘Hello ***’ , $string2
);

echo “$string1 – This should be Hello *** today\n”;
echo “$string2 – This is correct\n”;

Outputs:
======
Hello *** – This should be Hello *** today
Hello *** – This is correct

Leave a Reply

Your email address will not be published. Required fields are marked *