Part of the EllisLab Network
   
3 of 3
3
Language Parser Helper
Posted: 09 April 2008 07:55 AM   [ Ignore ]   [ # 21 ]  
Research Assistant
RankRankRank
Total Posts:  429
Joined  05-21-2007

humm you should build a small extentions with that, so many folks need that kind of setup, and wiki can’t give them.

i’ll be agree to payback like i’ve done for your HMVC for your support.

see ya around buddy.

 Signature 

-> None official irc channel [ irc.freenode.net #codeigniter ]

Profile
 
 
Posted: 21 April 2008 04:31 AM   [ Ignore ]   [ # 22 ]  
Summer Student
Avatar
Total Posts:  16
Joined  04-21-2008

Maybe a preg_match_all approach will be more efficient.

Profile
 
 
Posted: 21 April 2008 04:49 AM   [ Ignore ]   [ # 23 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

Maybe not. The language translation values need to be iterated.

But you’re free to modify the code to suit yourself.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 21 April 2008 05:19 AM   [ Ignore ]   [ # 24 ]  
Summer Student
Avatar
Total Posts:  16
Joined  04-21-2008
if (preg_match_all('/\{(\w*)\}/siU'$template$match))
{
  $count 
count($match[0]);
  for (
$i 0$i $count$i++)
  
{
    
if (($line $this->lang->line("$match[1][$i]")) === FALSE$line $match[1][$i];

    
$template str_replace($match[0][$i]$line$template);
  
}

With preg_match, if you have 20 vars, you do 20 regex comparisons. With preg_match_all, 1 regex comparison and 1 loop through a 20 items array.

Profile
 
 
Posted: 21 April 2008 06:55 AM   [ Ignore ]   [ # 25 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

Yep, Seems Ok. But why not use foreach instead of count() & for?

Also are you sure preg_match_all, doesn’t just use a repeated preg_match?

Maybe preg_replace_callback would be faster again?

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 21 April 2008 07:57 AM   [ Ignore ]   [ # 26 ]  
Summer Student
Avatar
Total Posts:  16
Joined  04-21-2008
wiredesignz - 21 April 2008 10:55 AM

Yep, Seems Ok. But why not use foreach instead of count() & for?

OK. As you wish.

if (preg_match_all('/\{(\w*)\}/siU'$template$matches))
{
  
foreach ($matches[0] as $key => $match)
  
{
      
if (($line $this->lang->line("$matches[1][$key]")) === FALSE$line $matches[1][$key];

      
$template str_replace($match$line$template);
  
}
wiredesignz - 21 April 2008 10:55 AM

Also are you sure preg_match_all, doesn’t just use a repeated preg_match?

Pretty sure. I tested it. For a small pack of vars (around 5) preg_match is faster, but preg_match_all wins at higher amounts of vars.

wiredesignz - 21 April 2008 10:55 AM

Maybe preg_replace_callback would be faster again?

Maybe. But then you need to write a function only to pass as parameter to preg_replace_callback or use create_function that makes the code hard to read.

Well, I am not a “performance Zealot”, but regular expressions are very slow and if calling functions inside loops is bad, calling regular expressions inside loops must be very bad.

Profile
 
 
Posted: 21 April 2008 08:18 AM   [ Ignore ]   [ # 27 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

Cool good work. You are welcome to add that to the wiki as an alternative method.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 21 April 2008 08:50 AM   [ Ignore ]   [ # 28 ]  
Summer Student
Avatar
Total Posts:  16
Joined  04-21-2008

No, thanks (multiple solutions to 1 problem = new problem).

Profile
 
 
   
3 of 3
3