Let’s say we are writing a custom templating system and we would like a link to be displayed where template contains {{link:http://example.com/something}}click here{{link}}.
What will be the most optimal way to parse this?
Some approaches I can think of: Simple string manipulation using substr. Using preg_replace with back substitution etc.
For my own parser, i’m using a mixture of “strpos / substr” and preg_replace. Why? well, its all in the numbers.
If your string to parse, contains alsmost only a {{link}} tag, without Start- & Endnoice, preg_replace is quicker e.g.
But, the more characters are in Endnoice, the slower preg_replace is, compared to substr / strpos 🙂
Just 100 characters in Endnoice, will make substr / strpos being quicker than preg_match!
The best way to proof this, is with code ofcause. So here is a testscript. It compares the sum of time for substr with preg_match
Even though substr requires way more code, it is more efficient 🙂
Greetz,
JB
p.s. some results:
.
.
.