Hi,

I have a little question that I need a bit of advice on.

Say I have a variable (maybe $userlist) that contains something like

someone:1
someoneelse:4
differentperson:2

and so on... say i have a variable, $username = 'someone'

I need to search through this other variable and see if there is a line where 'someone' exists, and if it does, i need to increment the number right of the : by 1.

But, I need this to match someone and not someoneelse, obviously.

I'm thinking that preg_match would be the best choice for searching to see if it exists, but my regex is very poor.. Here is something I have:

preg_match("/\b".$username."\b/i",$userlist)

Is this enough to match correctly? or do I need to include the : and the number in the matching case.

I also could use a little help on how to preg_replace the new number value onto that line.

Any help or advice you could offer would be appreciated.

Thanks,
Jay

    Hi,

    you can do something like this

    <?PHP
    $str ="someone:1\\nsomeoneelse:4\\ndifferentperson:2";
    EOTXT;
    
    $user = "someoneelse";
    
    $str = preg_replace_callback("§^(".$user.":)(.*?)\\$§msi",
           create_function('$matches','return $matches[1].($matches[2]+1);'),$str);
    echo $str;
    
    ?>
    

    Thomas

      Correction,

      use (\d?) instead of (.?) in the expression.

      Thomas

        Thank you for the reply, Thomas.

        This function works very well, but I need to do something else if the username has not matched against the list variable.

        I need to add a new line with the new username:1.

        so..

        if ( ) { // somehow match this username against the list
            // perform your function
        } else {
            $userlist = $userlist ."/n".$username.":1";
        }
        

        What would be a suitable if statement?

        Thanks,
        Jay

          <?PHP
          $str ="someone:1\\nsomeoneelse:4\\ndifferentperson:2";
          EOTXT;
          
          $user = "verydifferentperson";
          $pattern = "§^(".$user.":)(\\d*?)\\$§msi";
          if (preg_match($pattern,$str)) {
            $str = preg_replace_callback($pattern,
                 create_function('$matches','return $matches[1].($matches[2]+1);'),$str);
          } else {
            $str .= "\\n$user:1";
          }
          echo $str;
          ?>
          

          I hope I didn't mess up the backslashes ...

          Thomas

            Thanks Thomas! Works like a charm 🙂

            Just a quick question if it's not too much trouble: what does the EOTXT; line do?

            I've taken it out of my script and it still works as expected.

            Thanks again.

              You can ignore it (I forgot to remove it). I first had

              $str = <<<EOTXT
              someone:1
              someoneelse:4
              differentperson:2
              EOTXT;
              

              This is called heredoc syntax but some kind of overkill in this case.

              Thomas

                Cool thanks a lot - I learned a lot from your code 🙂

                  Note: if you want the code to be case sensitive then replace the pattern modifier msi by ms

                  Thomas

                    Using preg_replace_callback and a created function is overkill, this time (for one thing, it creates an entire function every time the code is run). All that's needed here is the /e modifier.

                    preg_replace('/^('.$user.'):(\\d+)$/mie', '"$1:".($2+1)', $str)

                      I sometimes tend to make things more complicated than neccessary 🙂

                        Write a Reply...