Help with this regular expression please

Discussion in 'PHP' started by JEET, Oct 28, 2008.

  1. #1
    Hi,
    This is what I have:
    preg_replace('/\$var([\.|>|<|=| |;|\+|\-|\*|\/|%|\(|\)|(^a-zA-Z0-9_)|]*)/i', 'something', $code);

    This should replace "$var=" to "something=".
    It is doing that, but it's also changing "$var123=" to "something123="

    How do I make sure that it only replaces if the next character is not an alphabet or number?
    I also tried:
    preg_replace('/\$'. $k. '([^a-zA-Z0-9_\x7f-\xff]*)/i', '\$'.$kk.'$1', $c);
    but that doesn't work either...

    Please help
    Thanks :)
     
    JEET, Oct 28, 2008 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi, could not be this enough ??
    preg_replace('/\$var=/i','something=',$str);
    It should replace only 'var' that directly follows '='
    As you don't want any number, alphabet (incl. underscore) as a next char, what else could be in variable name?
    You can do also something like preg_replace('/\$var[^\w=]*=/i','something=',$str); if you want to specify some exceptions:
    I don't know maybe I misunderstood the question...
     
    lp1051, Oct 28, 2008 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Hi,
    Thanks for the reply, but it's not just "$var" I want to replace. There are so many. Like:
    $var=
    this_one;
    $some + $$some_other_variable;

    Now see the last one for example.
    "$some" is different from "$some_other_variable".
    When I replace "$some", the other one gets replaced as well. There is another replacement for that one.

    What I want is that next character of match found should not be a "a-zA-Z0-9_". Then the replacement will work correctly I think...
    Any regular expression to do this?
    Thanks :)
     
    JEET, Oct 28, 2008 IP
  4. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Hi, ok, I see now better...
    In that case you should try to use word boundary for separating $some and $some_other_var
    Following example should work like - replace all variables specified by $k with $kk :
    preg_replace('/\$\b'. $k .'\b/i', '$'.$kk, $c);
    Is this better for your purpose?
     
    lp1051, Oct 28, 2008 IP
    JEET likes this.
  5. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    That worked like magic!!! :) :D
    Million thanks to you :)
    What does \b do here and while reading about regular expressions I also came across \s etc. Where can I read more on these?
    Thanks again.
    Green rep points added.
     
    JEET, Oct 29, 2008 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145