Is this possible? (just a question, no code)

Discussion in 'PHP' started by x0x, Oct 1, 2009.

  1. #1
    Let's say we have something like this in the database:

    "Player Jack attacked you and killed 1,000 hustlers, 284,234 bouncers and 0 thugs."

    Pure text. Would it be possible to make a script that pulls the numbers from the text and makes the variables?

    Like $deadhustlers = 1000; $deadbouncers = 284234; $deadthugs = 0;

    They would always be in the same order in the text. I don't really know of any functions that are able to do something.

    edit: I could also also put some random tags around the numbers like <deadthug>0</deadthug> thugs. If that would make it easier to pull them with another script?

    I don't want to log the dead "units" separately since they are already in the log text, but I will if it's my last option.

    What do you think?
     
    x0x, Oct 1, 2009 IP
  2. marshall_26

    marshall_26 Peon

    Messages:
    82
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
  3. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #3
    IMO the best solution would be to put some tags around the numbers and use preg_match to pull them out.
     
    Gray Fox, Oct 1, 2009 IP
  4. young coder

    young coder Peon

    Messages:
    302
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
  5. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Obviously xOx is trying to parse an external source (may be a oGame like site), so he won't be able to modify the text.
    Furthermore, as young coder suggested you can solve it easily with a regular expression:
    
    $line = 'Player Jack attacked you and killed 1,000 hustlers, 284,234 bouncers and 0 thugs.';
    if (preg_match('/([0-9,]+)[^0-9]+([0-9,]+)[^0-9]+([0-9,]+)/', $line, $result)) {
      var_dump($result);
    }
    
    PHP:
    $result will be:
    Array
    (
        [0] => 1,000 hustlers, 284,234 bouncers and 0
        [1] => 1,000
        [2] => 284,234
        [3] => 0
    )
    
    Code (markup):
     
    caprichoso, Oct 1, 2009 IP
  6. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Thanks guys, especially caprichoso, I'm going to use that method. Thanks again.
     
    x0x, Oct 1, 2009 IP