Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6347821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:17:53+00:00 2026-05-24T21:17:53+00:00

I’m using a PHP template engine I’ve written some time ago. It relies on

  • 0

I’m using a PHP template engine I’ve written some time ago. It relies on regexes to create a cached PHP file. Some examples of the syntax:

{$foo} - regular variable
{$foo.bar} - variable foo that uses the array key 'bar'
{$foo|uppercase} - modifier 'uppercase' that takes 'foo' and applies some method to it

{iteration:users}
    Hi there {$users.name}
{/iteration: users}

The list goes on… There’s quite an amount of nasty regexes involved to parse all this. Note that an iteration can be inside another iteration and so on.

Recently I’ve been seeing template engines like twig, smarty3, that use a template lexer. I have a few questions about this:
– In general isn’t the lexer way slower than using a few regexes to create a cached php template?
– Are there good resources on how to write your own lexer to interpret some sort of (template) language (I couldn’t find anything I understand on google)
– Should I keep using regexes or is a lexer something worth exploring?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:17:54+00:00Added an answer on May 24, 2026 at 9:17 pm

    I suggest writing Parsing expression grammars (PEGs), and see this answer for a PEG library in PHP.

    PEGs are very much alike Regular Expressions, they are greedy by nature, and never ambiguous: great for a Domain Specific Language (DSL).

    In general isn’t the lexer way slower than using a few regexes to create a cached php template?

    No: the speed of regular expressions are implementation dependent of the Regular Expression engine. In general, every time you use a Regular Expression, it needs to be parsed itself, and then with the given model, it must use a general matcher, that works with all Regular Expressions possible.

    Given a lexer, you fine-tune the matcher: you get a specific matcher, which only works for your predefined grammar. One gain is in the bootstrap case: no need to compile the Regular Expression. Another gain is in it’s lesser complexity, due to it’s specific matcher, which tends to run faster.

    Are there good resources on how to write your own lexer to interpret some sort of (template) language (I couldn’t find anything I understand on google)?

    Lexers are quite complex. To write your own you will have to know stuff about state machines, regular grammar, context-free or non-context-free grammers, etc.

    It requires some fundamental computer science knowledge before it’s easy to grasp though.

    Should I keep using regexes or is a lexer something worth exploring?

    Worth noting is the error-catching capabilities of well engineered lexers (e.g. an error message: “expected ;, but found ), on line 64:38.”)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to create an if statement in PHP that prevents a single post
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.