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

Posted by jerrygarciuh on 06/08/07 17:33

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

 

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

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