You are here: Re: Pattern Matching When $haystack _may_ be an array « PHP Programming Language « IT news, forums, messages
Re: Pattern Matching When $haystack _may_ be an array

Posted by ZeldorBlat on 06/08/07 20:39

On Jun 8, 1:33 pm, jerrygarciuh <jerrygarc...@gmail.com> wrote:
> Hello,
>
> I am converting a large scale app from Perl TemplateToolkit and
> Class::DBI to PHP.
>
> TemplateToolkit has elegant ways of coping with variables that may be
> a scalar or may be an array. So my predecessor has a number of vars
> that are ambiguous in this way.
>
> My question to you is whether, given this ambiguity, there is a better
> way to do this in PHP than my current solution?
>
> // $a is sometimes an array
> $a = array('foo', 'bar', 'kermit ruffins');
>
> if (is_array($a)) {
> foreach($a as $b) {
> if (preg_match('/ruffin/', $b)) {
> echo 'ruffin found';
> }
> }
> } else {
> if (preg_match('/ruffin/', $a)) {
> echo 'ruffin found';
> }
> }
>
> Much obliged for any help!
>
> Peace,
>
> JG

Just make it an array if it isn't then handle it as an array
everytime:

// $a is sometimes an array
$a = array('foo', 'bar', 'kermit ruffins');

if(!is_array($a))
$a = array($a);

foreach($a as $b) {
if (preg_match('/ruffin/', $b)) {
echo 'ruffin found';
}
}

 

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

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