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

  • SEARCH
  • Home
  • 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 8939855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:54:37+00:00 2026-06-15T10:54:37+00:00

Let’s say we are writing a custom templating system and we would like a

  • 0

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.

  • 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-06-15T10:54:38+00:00Added an answer on June 15, 2026 at 10:54 am

    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.

    STARTNOICE{{link:http://example.com/something}}click here{{link}}ENDNOICE
    

    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:

    Startnoice = Endnoice = 100, /w 100 loops
    $substrTime:
    0.004448 Seconds
    $pregTime:
    0.004752 Seconds
    

    .

    Startnoice = Endnoice = 1000, /w 100 loops
    $substrTime:
    0.005598 Seconds
    $pregTime:
    0.023844 Seconds  << waaaay slower
    

    .

    Startnoice = Endnoice = 10000, /w 100 loops
    $substrTime:
    0.009028 Seconds
    $pregTime:
    0.278836 Seconds  << Dont use preg_replace
    

    .

    <?PHP
    $startNoice = 1;
    $endNoice   = 100;
    
    for( $jb = 0; $jb < 100; $jb++ )
    {
        $string = randomgenerator( $startNoice ) . '{{link:http://example.com/something}}click here{{link}}.' . randomgenerator( $endNoice );
    
    /**************/
    // Strpos / substr
    /**************/
    
    /* Start the timer */
        $Start = '{{link:';
        $StartEnd = '}}';
        $Stop = '{{link}}';
    
        $StartLen = strlen( $Start );
        $StartEndLen = strlen( $StartEnd );
        $StopLen = strlen( $Stop );
    
        $time_start = microtime_float();
        $strpos['Start']    = strpos( $string, $Start );
    
        $start = substr( $string, 0, $strpos['Start']);
        $end = substr( $string, $strpos['Start'] );
    
        $strpos['StartEnd'] = strpos( $end, $StartEnd, $StartLen );
        $strpos['Stop']     = strpos( $end, $Stop, $StartLen );
    
        $url  = substr( $end, $strpos['Start'] + $StartLen, ($strpos['StartEnd'] - $strpos['Start'] - $StartLen)  );
        $text = substr( $end, $strpos['StartEnd'] + $StartEndLen, ($strpos['Stop'] - $strpos['StartEnd'] - $StartEndLen) );
    
    // Replace the link
        $NewString1 = $start . '<a href="' . $url . '">' . $text . '</a>' . substr( $end, $strpos['Stop'] + $StopLen );
    /* Stop the tumer */
        $time_end = microtime_float();
        $substrTime[] = $time_end - $time_start;
    
    
    /**************/
    // Preg_match
    /**************/
        $time_start = microtime_float();
        $NewString2 = preg_replace('/{{link:(.*)}}(.*){{link}}/', '<a href="$1">$2</a>', $string);
        $time_end = microtime_float();
        $pregTime[] = $time_end - $time_start;
    }
    
    echo '$substrTime: . <br />';
    echo round( array_sum( $substrTime ), 6) . ' Seconds';
    echo '<br />';
    echo '$pregTime: . <br />';
    echo round( array_sum( $pregTime ), 6) . ' Seconds';
    
    function randomgenerator( $length )
    {
        $string = 'abcdefghijklmnopqrstuvwxyz01234567890!@#$%^&*()_+=-{}][|\::",./';
        $r = '';
        for( $i = 0; $i < $length; $i ++ )
        {
            $r .= $string[ rand(0, strlen( $string ) -1 ) ];
        }
        return $r;
    }
    
    function microtime_float()
    {
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say I've this URL: http://example.com/image-title/987654/ I want to insert download to the part
Let's have an example like below: package xliiv.sandbox; import android.app.Activity; import android.os.Bundle; import android.util.Log;
Let me explain best with an example. Say you have node class that can
Let me try to explain by example. Say website is hosted at example.com (NOT
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say I can call a method like this: core::get() . What is the
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2

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.