Jump to content

simple way to get values from a predefined template


opencombatclan

Recommended Posts

Hello everyone, here I am once again.

 

This time I need to know something about reading values from a predefined template.

Lets say I have got the following template

[TEST:1531222 12.11.2009] 35f8519d1980ccfc771A0e5986af5e06 "name" "ip" "type"

And I need the following variables from this string.

 

$date

$hash

$name

$ip

$type

 

Some more info about the string:

the first part (TEST) is always the same

then a number (which can be short or long) follows

then a date

then a hash

then the name (which can be short or long)

then a ip (which can be short or long)

then a type (which can be short or long)

An empty string looks like:

[TEST: ]  "" "" ""

 

I hope someone can help me  :D

Maybe a regular expression would do the trick?

Link to comment
Share on other sites

It's difficult from your example to work out exactly what's there and what's simply to get your point across, but it seems like everything is there (including brackets and quotes), so that's what I'm going to go with.

 

$input = '[TEST:1531222 12.11.2009] 35f8519d1980ccfc771A0e5986af5e06 "name" "ip" "type"';
preg_match('#\[TEST:([0-9]*) ((?:[0-9]{2}\.){2}[0-9]{4})\] ([a-z0-9]{32}) "([^"]*)" "([^"]*)" "([^"]*)"#i', $input, $out);
echo '<pre>';
print_r($out);
echo '</pre>';

Link to comment
Share on other sites

You're welcome. By the way, thanks for actually stating your example clearly, specifying parameters for each match, and giving an example input string. It's not often we get that and it's so much easier to help people when they do. You could if you wanted narrow it down more or actually validate dates (within certain tolerances) or the IP the worlds your oyster, but I'm assuming none of that is really a requirement.

 

EDIT: Oh and if you consider this solved, click the 'Mark Solved' button (bottom left of threads you start.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.