Welcome Guest, Not a member yet? Register   Sign In
Language Parser Helper
#21

[eluser]wiredesignz[/eluser]
Thanks sikkle, it would be quite easy to use routing to extract the language abbreviation and then use that value to set the $lang_files and $lang values for the parser.

Something like this maybe:
Code:
//route: en/home => home->index('en');
$route['(\w*)/(.*)'] = '$2/index/$1';
#22

[eluser]sikkle[/eluser]
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.
#23

[eluser]arume[/eluser]
Maybe a preg_match_all approach will be more efficient.
#24

[eluser]wiredesignz[/eluser]
Maybe not. The language translation values need to be iterated.

But you're free to modify the code to suit yourself.
#25

[eluser]arume[/eluser]
Code:
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.
#26

[eluser]wiredesignz[/eluser]
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?
#27

[eluser]arume[/eluser]
[quote author="wiredesignz" date="1208793325"]Yep, Seems Ok. But why not use foreach instead of count() & for?[/quote]

OK. As you wish.
Code:
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);
  }
}

[quote author="wiredesignz" date="1208793325"]Also are you sure preg_match_all, doesn't just use a repeated preg_match?[/quote]

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.

[quote author="wiredesignz" date="1208793325"]Maybe preg_replace_callback would be faster again?[/quote]

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.
#28

[eluser]wiredesignz[/eluser]
Cool good work. You are welcome to add that to the wiki as an alternative method.
#29

[eluser]arume[/eluser]
No, thanks (multiple solutions to 1 problem = new problem).




Theme © iAndrew 2016 - Forum software by © MyBB