Reply to Re: parsing template using preg_replace_callback()

Your name:

Reply:


Posted by mens libertina on 08/18/06 18:18

Chung Leong wrote:
> What you need to do is fake a closure by passing an object. Example
>
> class KeywordContainer {
> var $hash;
>
> function KeywordContainer($hash) {
> $this->hash = $hash;
> }
>
> function PCRESubstitute($m) {
> $key = strtolower($m[1]);
> if(array_key_exists($key, $this->hash)) {
> return $this->hash[$key];
> } else {
> return $m[0];
> }
> }
> }
>
> $user = array('user' => "morgan",
> 'name' => "Morgan Seppy",
> 'expires' => "2006-08-28" );
> $closure = array(new KeywordContainer($user), 'PCRESubstitute');
>
> $template = "Hello, {NAME}! {USER} expires on {EXPIRES}";
> echo preg_replace_callback('/\{(\w+?)\}/', $closure, $template);

I don't know anything about closures, so I will look this up. Thanks!

[Back to original 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

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