SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question preg_replace_callback vs while loop line by line for template compiler

    Hello ,

    i built php class for parsing html templates , and i used preg_replace_callback function for parsing template while loops & if conditions , for loop , ... etc

    i repeated the function many times , for every process , such as one time for parsing while loop , and another time for if conditions parsing .. etc , and repeated all of these steps again when parsing include files inside the template ,

    but i think that this method of parsing is slow when parsing big template ,

    so i think that using one while loop and read template line by line and compare it then parsing found line will be more fast ,

    so what is your opinion , using one while loop and read the template file line by line and parsing it is more useful ? or using preg_replace_callback ?

    and is it preferred to parse the codes inside the template ? by replacing it with php code then evaluate the template using eval() ? or parsing it inside the php file then append the result to the template ?

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You seemed to already know the answer! Using a while loop will be much quicker. If you are concerned with speed of operations maybe grab a php profiler and run your own tests on speed.
    coming soon sitejuju.com my new development portfolio

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Better still is to just accept that PHP is a template engine and not try to build a template engine in it. As I've said before it's like buying a truck to tow around a truck - inefficient at best.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •