preg_match - How do I...?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Falcon879
Forum Newbie
Posts: 2
Joined: Mon Nov 29, 2010 2:50 am

preg_match - How do I...?

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: preg_match - How do I...?

Post 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
}
Post Reply