Page 1 of 1

preg_match - How do I...?

Posted: Mon Nov 29, 2010 2:53 am
by Falcon879
Say I want to find in a string the word 'car' and after that it can be anything any lenght but the word 'hotel' and after that it can be anything any lenght and then the word 'hello' should come.

How would that perl regex look like?? How do I write to not allowing a word like 'hotel' inside this string?

Re: preg_match - How do I...?

Posted: Mon Nov 29, 2010 3:18 am
by requinix
Perl? Methinks you're asking in the wrong site.
Wrong forum, at the very least, since there's a dedicated regex forum.

strpos is your friend, for both "can't have the word 'hello'" and "must have 'car', then somewhere after a 'hello' but no 'hotel'".

Code: Select all

$car = strpos($string, "car");
$hotel = strpos($string, "hotel", $car);
$hello = strpos($string, "hello", $car);
if ($car !== false && $hotel === false && $hello !== false) {
    // valid
}