I Need help in this function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Drago
Forum Newbie
Posts: 3
Joined: Mon Jan 09, 2012 8:27 am

I Need help in this function

Post 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]
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: I Need help in this function

Post 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.
Drago
Forum Newbie
Posts: 3
Joined: Mon Jan 09, 2012 8:27 am

Re: I Need help in this function

Post 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
Drago
Forum Newbie
Posts: 3
Joined: Mon Jan 09, 2012 8:27 am

Re: I Need help in this function

Post by Drago »

so it possible or ?
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: I Need help in this function

Post 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 ")".
Post Reply