preg_replace with "eval"?

From: Sven (google_at_hofrichter.net)
Date: 10/20/04

  • Next message: pat: "regular expression to replace \n with <br>"
    Date: 20 Oct 2004 05:10:09 -0700
    
    

    Hi,

    I tried to code a "one-line"-replacment-function, that put a found
    value into a variable and deletes it from string. I know, it is
    possible to do it by using a help-function (example 2), but I'm
    looking for a "shorter" way.

    $subject = "Re: PUT: an answered mail";

    [example 1]
    $subject = preg_replace(
    "/^((Re|AW):\s*)/ie","\$isanswer='\$1'",$subject );
    => I wanted to get:
       - $isanswer="Re: "
       - $subject ="PUT: an answered mail"
    => but I got:
       - $isanswer="Re: "
       - $subject ="Re: PUT: an answered mail"

    [example 2]
    $subject = preg_replace(
    "/^((Re|AW):\s*)/ie","setAnswer(\$1)",$subject );
    function setAnswer( $val ) { \$isanswer='\$1'; return ""; }
    -> I wanted to get and got it:
       - $isanswer="Re: "
       - $subject ="PUT: an answered mail"

    Any ideas to filter it shorter (with an higher performance).


  • Next message: pat: "regular expression to replace \n with <br>"

    Relevant Pages

    • Re: 1st and 2nd.... positions
      ... It took your posting the idea of searching a string for the correct position to give me a nudge. ... Can the shorter main text string in the second formula really add significantly to the efficiency of performing the MID function call that it can compensate for the extra function call? ...
      (microsoft.public.excel.misc)
    • Re: Replacement Strat Machineheads
      ... > Two are the same lengths as the original tuners and four are shorter - ... > especially on the D string. ... > four shorter tuners were for the strings that go via the trees with the ... string angle for the higher 4 strings and eliminate the string trees for a ...
      (rec.music.makers.guitar)
    • Re: Collation....
      ... If you use varying-length (varchar or nvarchar), ... on comparison the shorter string will be padded to ...
      (microsoft.public.sqlserver.programming)
    • Re: code for Computer Language Shootout
      ... > mainisn't useful for this very short loop, and you can use shorter ... import string, itertools, sys ... reverse, and format the string. ...
      (comp.lang.python)
    • Re: The best, (right?), way to trim a char*
      ... >> Good you give us an example of shorter code. ... there is no need to check against npos because the ... > return String; ... the TrimLeft function you originally provided didn't handle the empty ...
      (comp.lang.cpp)