[Speed] Multiple scans through variable for search/replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erwin Moller

    [Speed] Multiple scans through variable for search/replace

    Hi Group,

    At the moment I am developing a app that uses templates.
    In the templates I have placeholders like this:

    Dear ***FIRSTNAME*** <br>
    You have subscribed to ***LISTSUBSRIBE ***.
    etc

    The ***XXXX*** must be replaced with values created in my script.

    I am a bit worried about the speed and wonder which approach will be
    fastest.
    A straightforward solution would be to run a
    str_replace($ar rSearch,$arrRep lace), where
    $arrSearch contains the strings to be replaced by the corresponding values
    in $arrReplace.

    Is this a fast solution?
    Will a regexpression approach be faster?

    Does anybody have experience with this or advise?
    Or should I just try a few and benchmark it?

    TIA!

    Regards,
    Erwin Moller
  • petersprc

    #2
    Re: Multiple scans through variable for search/replace

    Hi,

    You probably won't notice any performance issues unless you have
    enormously high request volume... str_replace would be faster than a
    regex in general. There's also Smarty templates and straight PHP like
    so:

    mailer.php:

    foreach ($subscriptions as $sub) {
    ob_start();
    require('email-template.php');
    $text = ob_get_contents ();
    ob_end_clean();
    mail($sub['email'], 'Subscription Confirmation', $text,
    "From: My Site <info@mysite.co m>");
    }

    email-template.php:

    Dear <?= $sub['name'] ?>: You subscribed to <?= $sub['list'] ?>.

    Erwin Moller wrote:
    Hi Group,
    >
    At the moment I am developing a app that uses templates.
    In the templates I have placeholders like this:
    >
    Dear ***FIRSTNAME*** <br>
    You have subscribed to ***LISTSUBSRIBE ***.
    etc
    >
    The ***XXXX*** must be replaced with values created in my script.
    >
    I am a bit worried about the speed and wonder which approach will be
    fastest.
    A straightforward solution would be to run a
    str_replace($ar rSearch,$arrRep lace), where
    $arrSearch contains the strings to be replaced by the corresponding values
    in $arrReplace.
    >
    Is this a fast solution?
    Will a regexpression approach be faster?
    >
    Does anybody have experience with this or advise?
    Or should I just try a few and benchmark it?
    >
    TIA!
    >
    Regards,
    Erwin Moller

    Comment

    • Erwin Moller

      #3
      Re: Multiple scans through variable for search/replace

      petersprc wrote:
      Hi,
      >
      You probably won't notice any performance issues unless you have
      enormously high request volume... str_replace would be faster than a
      regex in general. There's also Smarty templates and straight PHP like
      so:
      >
      mailer.php:
      >
      foreach ($subscriptions as $sub) {
      ob_start();
      require('email-template.php');
      $text = ob_get_contents ();
      ob_end_clean();
      mail($sub['email'], 'Subscription Confirmation', $text,
      "From: My Site <info@mysite.co m>");
      }
      >
      email-template.php:
      >
      Dear <?= $sub['name'] ?>: You subscribed to <?= $sub['list'] ?>.
      Hi,

      Thanks for your response.
      I'll stick to str_replace then. :-)

      I cannot use direct writes/echo to the template in my situation. The
      customer wants to see as little PHP as possible, so I must stick to the
      ***XXXX*** thingy. Nice/smart setup though. :-)

      Regards,
      Erwin Moller
      >
      Erwin Moller wrote:
      >Hi Group,
      >>
      >At the moment I am developing a app that uses templates.
      >In the templates I have placeholders like this:
      >>
      >Dear ***FIRSTNAME*** <br>
      >You have subscribed to ***LISTSUBSRIBE ***.
      >etc
      >>
      >The ***XXXX*** must be replaced with values created in my script.
      >>
      >I am a bit worried about the speed and wonder which approach will be
      >fastest.
      >A straightforward solution would be to run a
      >str_replace($a rrSearch,$arrRe place), where
      >$arrSearch contains the strings to be replaced by the corresponding
      >values in $arrReplace.
      >>
      >Is this a fast solution?
      >Will a regexpression approach be faster?
      >>
      >Does anybody have experience with this or advise?
      >Or should I just try a few and benchmark it?
      >>
      >TIA!
      >>
      >Regards,
      >Erwin Moller

      Comment

      Working...