Page 1 of 1

I Need help in this function

Posted: Mon Jan 09, 2012 8:34 am
by Drago
[img]hey
i want a function do that,
select the action / name to be output : 2 => Weapon Mastery

Code: Select all

( skill

	( name  "Weapon Mastery")

	( action 2)

	( image	"passive001")

)
( skill


	( name  "Lightning Slash")

	( action 3)

	( image	"active003")

)
i tried that

Code: Select all

function extract($string){

preg_match_all("/action [0-9]+/i", $string, $matches);
  return $matches[0];

}

$outs = extract($text);


$ago =  (implode($outs,",<br>"));

print(preg_replace('/action/','',$ago));
it successed and output is :
2
3
but i didnt find the way to select the action / name in same time
to be outputs : 2 => Weapon Mastery[/img]

Re: I Need help in this function

Posted: Mon Jan 09, 2012 1:33 pm
by tr0gd0rr
If you're sure the output will be in that exact format, you could use

Code: Select all

preg_match_all('/name\s+"(.*?)"\)\s+\(\s+action\s+(.*?)\)/', $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
  list (, $name, $action) = $match;
  if ($action == 2) {
    echo $name;
  }
}
Otherwise you might want something more robust that splits out the string into an array of associative arrays by looking at parenthesis.

Re: I Need help in this function

Posted: Mon Jan 09, 2012 3:43 pm
by Drago
tr0gd0rr wrote:If you're sure the output will be in that exact format, you could use

Code: Select all

preg_match_all('/name\s+"(.*?)"\)\s+\(\s+action\s+(.*?)\)/', $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
  list (, $name, $action) = $match;
  if ($action == 2) {
    echo $name;
  }
}
Otherwise you might want something more robust that splits out the string into an array of associative arrays by looking at parenthesis.
thx alot
it working fine
i want ask about another thing, if it possible

Code: Select all

(item(name 4025)(desc 4001)(Index 4028)(Image "defs5v"))
(item(name 4026)(desc 4002)(Index 4029)(Image "defs5j"))
(item(name 4027)(desc 4003)(Index 4030)(Image "defs5s"))

Code: Select all

( itemname 4025 "Shiun [Shield]")
( itemname 4026 "Adorned Shiun [Shield]")
( itemname 4027 "Ornate Shiun [Shield]")
its possible to select item name and index for example
Index 4028 => 4025 "Shiun [Shield]" ((name 4025 = itemname 4025))
Index 4029 => 4026 "Adorned Shiun [Shield]" ((name 4026 = itemname 4025))

Index 4030 => 4027 "Ornate Shiun [Shield]"

is that possible
i tried much time with preg_match_all function, but failed

Re: I Need help in this function

Posted: Tue Jan 10, 2012 7:46 am
by Drago
so it possible or ?

Re: I Need help in this function

Posted: Tue Jan 10, 2012 2:11 pm
by tr0gd0rr
You can certainly parse those strings with regexes. At this point, you probably want to make a more general function. A regex example to start to chunk your first example would be /\(item((?:\(.+?\))+)\)/ which says to look for (%value%) groups inside of (item%groups%) text. Then you would maybe want to split those chunks by "(" and trim ")".