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?

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>';

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.

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. ...

 

:qft:

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.