You are here: Re: newbie alert: how to write this in a shorter way « PHP Language « IT news, forums, messages
Re: newbie alert: how to write this in a shorter way

Posted by Rik on 12/20/06 12:18

Geoff Berrow wrote:
> Message-ID: <15270vgkskofw$.ex7bxqbswi4v.dlg@40tude.net> from Els
> contained the following:
>
>> I'm just looking for a way to write the following in a shorter way:
>> if ($a == 1 || $a == 2 || $a == 3 || $a == 4 || $a == 17 || $a == 30)
>>
>> Something that equates to "if $a == one of ((1 - 4), 17, 30)"
>
> I've just started playing round with regexp so this may not be optimal
>
> $pattern = '/(?<![0-9])([1-4]|17|30)(?![0-9])/';
> if(preg_match($pattern, $a)>0){
> //do stuff
> }

Well, I wouldn't use a regex for this, but if I did:
$pattern = '/^(?:[1-4]|17|30)$/';
We're not looking for a number in a string, but rather matching the entire
variable. And offcourse not capturing as we're not going to make use of
matches further on.
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация