#1
  1. Banned

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,326
    Rep Power
    0

    What does this pattern do?


    I don't think I need this pattern - what's it doing? It uses html entites so I added a space before the semi-colons:
    PHP Code:
    $value preg_replace(array('/\$([0-9])/''/\\\\([0-9])/'), array('&#36 ;\1''&#92 ;\1'), $value); 
    Thanks,
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,885
    Rep Power
    6354
    It's replacing $4 with &#36 ;4, and \4 with &#92 ;4. Naturally the '4' can be any digit.

    Since there really isn't any reason for this aside from going overboard on HTML encoding, I vote for "you don't need it."

    However, if you're going to be running a string through exec() or something...you may need it. But don't do that.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Banned

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,326
    Rep Power
    0
    Thanks,

    I'm re-writing the PHPLIB template class - and I'm sure I can change some of these preg_ functions for str_replace().


    A finish() function reverses the first replace! However I commented out these preg_replace() calls and eveything seemed to work still - except much faster!
    PHP Code:
    function set_var()
    {
       
    // $value = preg_replace(array('/\$([0-9])/', '/\\\\([0-9])/'), array('$\1', '\\1'), $value);
           
       
    $this->varkeys[$varname] = '/'.$this->varname($varname).'/';
            
       
    $this->varvals[$varname] = $value;
    }

    function 
    finish()
    {
          switch (
    $this->unknowns)
          {
              case 
    'keep':
              break;
              
              case 
    "remove":
              
    $str preg_replace('/{{[^\s\t\r\n}]+}}/'''$str);
              break;

              case 
    'comment':
              
    $str preg_replace('/{{([^\s\t\r\n}]+)}}/'"<!-- Template variable \\1 undefined -->"$str);
              break;
          }

          
    // $str = preg_replace(array('/$([0-9])/', '/\([0-9])/'), array('$\1', '\\\1'), $str);
          
          
    return $str;

    Is it something to do with back-references?

    Also do you know which of these patterns is likely to be faster?
    PHP Code:
    $pattern1 '/{{([^\s\t\r\n}]+)}}/';

    $pattern2 '/{{([A-Z0-9]+)}}/'
    Cheers
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,885
    Rep Power
    6354
    I don't think it has anything to do with back-references, no.
    PHP Code:
    $pattern1 '/{{([^\s\t\r\n}]+)}}/';

    $pattern2 '/{{([A-Z0-9]+)}}/'
    They do different things. One matches anything that's not a whitespace or a }. The second matches capital letters and numbers only. So the first one matches "#@Hello_!--&," while the second will not. I can't speak to the speed of either one, I'd assume they would be close enough that it wouldn't matter.

    -Dan

    Comments on this post

    • derelict agrees
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. Banned

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,326
    Rep Power
    0
    Thanks,

    The patterns are trying to match template variable names. The variables are formatted like {{VAR1_NAME}}.

    Variable names will only ever consist of letters, numbers and underscores so I think pattern2 is more suitable.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,885
    Rep Power
    6354
    Oh good, then yes the second pattern is best. Note that it's case-sensitive too.

    Comments on this post

    • b3n agrees
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. Banned

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,326
    Rep Power
    0
    Yep - variables are always upper case.

    Originally the template class enclosed variables in single braces, which caused it to strip out inline css/javascript. So I changed the class to use double braces instead.

    That would probably be enough but the class has not been updated since 2002 and is less than 500 lines of code, so I thought I'd try to improve it

    Thanks for the help

IMN logo majestic logo threadwatch logo seochat tools logo