Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Wyrażenia reuglarne - preg_match(_all) nic nie znajduje
Forum PHP.pl > Forum > Przedszkole
infoo1
  1. <?php
  2. preg_match_all('/<!-- BEGIN test -->(.*?)<!-- END test -->/','<?php echo $this->_VARS['TEST']; ?>
  3. <br />
  4. <?php $this->_include('plik_testowy.tpl'); ?>
  5. <!-- BEGIN test -->
  6. {test.AAA}
  7. <!-- END test -->',$matches2);
  8. if(preg_match($pattern,$this->content)){echo 'ok';}else{echo '!ok';}
  9. ?>

Wyświetla "!ok". Print_r($matches2) daje tablicę złożoną z 2 pustych tablic. Co jest źle?
I $pattern, $this->content są takie same jak w preg_match_all.

Cały plik (błąd występuje w template_compile::parse_loops):
  1. <?php
  2.    header('content-type: text/plain');
  3.    class template_compile
  4.    {
  5.        public function __construct($dir)
  6.        {
  7.            $this->dir        =    $dir;
  8.            return true;
  9.        }
  10.  
  11.        public function compile()
  12.        {
  13.            $files    =    glob($this->dir . 'tpl_*.tpl');
  14.            foreach ($files as $file_id => $file_name)
  15.            {
  16.                $this->compile_exec($file_name);
  17.            }
  18.  
  19.            return true;
  20.        }
  21.  
  22.        public function compile_file($file_name)
  23.        {
  24.            $this->compile_exec($tis->dir . 'tpl_' . $file . '.tpl');
  25.            return true;
  26.        }
  27.  
  28.        private function compile_exec($file)
  29.        {
  30.            if(!is_file($file))
  31.            {
  32.                trigger_error('File' . $file . 'doesn't exist!',E_USER_ERROR);
  33.            }
  34.            $this->content    =    file_get_contents($file);
  35.            $this->parse_vars();
  36.            $this->parse_includes();
  37.            $this->parse_ifs();
  38.            $this->parse_loops();
  39.            return true;
  40.        }
  41.  
  42.        private function parse_vars()
  43.        {
  44.            preg_match_all('/{(.*?)}/',$this->content,$matches);
  45.            foreach ($matches[0] as $id => $search)
  46.            {
  47.                if(!preg_match('/(.*?).(.*?)/',$search))
  48.                {
  49.                    $this->content    =    str_replace($search,'<?php echo $this->_VARS['' . $matches[1][$id] . '']; ?>',$this->content);
  50.                }
  51.            }
  52.  
  53.            return true;
  54.        }
  55.  
  56.        private function parse_includes()
  57.        {
  58.            preg_match_all('/<!-- INCLUDE (.*?) -->/',$this->content,$matches);
  59.            foreach ($matches[1] as $id => $file)
  60.            {
  61.                $this->content    =    str_replace($matches[0][$id],'<?php $this->_include('' . $file . ''); ?>',$this->content);
  62.            }
  63.  
  64.            return true;
  65.        }
  66.  
  67.        private function parse_ifs()
  68.        {
  69.            preg_match_all('/<!-- IF (.*?) -->/',$this->content,$matches);
  70.            foreach ($matches[1] as $id => $if)
  71.            {
  72.                //
  73.            }
  74.  
  75.            return true;
  76.        }
  77.  
  78.        private function parse_loops()
  79.        {
  80.            preg_match_all('/<!-- BEGIN (.*?) -->/',$this->content,$matches);
  81.            foreach ($matches[1] as $id => $loop_name)
  82.            {
  83.                $pattern    =    '/<!-- BEGIN ' . $loop_name . ' -->(.*?)<!-- END ' . $loop_name . ' -->/';echo $pattern;  echo $this->content;
  84.                preg_match_all($pattern,$this->content,$matches2);if(preg_match($pattern,$this->content)){echo 'ok';}else{echo '!ok';}   die;
  85.                foreach ($matches2[1] as $id2 => $code)
  86.                {
  87.                    $new_code    =    $code;
  88.                    preg_match_all('/{(.*?)}/',$this->content,$matches);
  89.                    foreach ($matches2[1] as $id123 => $search)
  90.                    {
  91.                        if(preg_match('/(.*?).(.*?)/',$search))
  92.                        {
  93.                            $new_code    =    str_replace($search,'<?php echo $this->_BLOCK_VARS[' . $loop_name . '][' . $matches2[1][$id123] . ']; ?>',$new_code);
  94.                        }
  95.                    }
  96.  
  97.                    $this->content    =    preg_replace('/<!-- BEGIN ' . $loop_name . ' -->' . $new_code . '<!-- END ' . $loop_name . ' -->/','<?php foreach ($this->_BLOCK_VARS['' . $loop_name . ''] as $id => $value){eval('<?php' . ' . $new_code . ' . '?>');} ?>',$this->content);
  98.                }
  99.            }
  100.  
  101.            return true;
  102.        }
  103.    }
  104. ?>
  105.  
  106.  
  107. <?php
  108.    $tc    =    new template_compile('tpl/');
  109.    $tc->_VARS['test']    =    'zmienne: działają';
  110.    $tc->_BLOCK_VARS['test']    =    array(
  111.        'test.AAA'    =>    'loopy ok'
  112.    );
  113.    $tc->compile();
  114.    echo $tc->content;
  115. ?>
tpl/test.tpl:
Kod
{TEST}
       <!-- INCLUDE plik_testowy.tpl -->
       <!-- BEGIN test -->
       fghj{test.AAA}aa
       <!-- END test -->
nospor
chodzi o nowe linie. domyslnie wyrazenie regularne dotyczy tylko jednej linii. musisz dopisac /s
  1. <?php
  2. preg_match_all('/<!-- BEGIN test -->(.*?)<!-- END test -->/s','<?php echo $this->_VARS['TEST']; ?>
  3. <br />
  4. <?php $this->_include('plik_testowy.tpl'); ?>
  5. <!-- BEGIN test -->
  6. {test.AAA}
  7. <!-- END test -->',$matches2);
  8. print_r($matches2);
  9. ?>

A na przyszlosc:
http://pl.php.net/manual/pl/reference.pcre...n.modifiers.php
masz tam to wszystko opisane
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.