Jump to content

[SOLVED] PHP Parser


Minase

Recommended Posts

hy there i was wondering how to create a php parser for those codes,it seem that whatever i tryed it doesnt work ...

 

(npc (name 1) (index 1) (country 252) (race 0) (level 1) (ai 1) (range 20) (sight 160 240) (exp 2) (itemgroup 1 2)
	(str 21) (hth 1) (int 10) (wis 10) (dex 2) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 7 7) (magic 0 0) (defense 0 0) (absorb 0) (mspeed 1600 800)
	(quest (2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)))

(npc (name 2) (index 2) (country 252) (race 0) (level 3) (ai 1) (range 16) (sight 160 240) (exp 4) (itemgroup 2 2)
	(str 27) (hth 2) (int 10) (wis 10) (dex 4) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 8  (magic 0 0) (defense 1 1) (absorb 0) (mspeed 1600 800) (resist 0 0 1 1 1)
	(quest (2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)))

 

i need to create a query for every NPC

 

like :

INSERT INTO NPC (`name`, `index`, `country` ..... etc etc) VALUES (1,1,252......etc etc);

 

something like that for every npc

Note: some npc can have quest null like this "(quest ))" and it will end just in 2 ")" not in 3.

 

a basic ideea would be awesome

 

thank you very much

Link to comment
Share on other sites

as you can see here i have 2 NPC that must be parsed in 2 querys

<!--  FIRST QUERY START
(npc (name 1) (index 1) (country 252) (race 0) (level 1) (ai 1) (range 20) (sight 160 240) (exp 2) (itemgroup 1 2)
(str 21) (hth 1) (int 10) (wis 10) (dex 2) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
(attack 0 7 7) (magic 0 0) (defense 0 0) (absorb 0) (mspeed 1600 800)
(quest (2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)))
FIRST QUERY END -->

<!-- SECOND QUERY START
(npc (name 2) (index 2) (country 252) (race 0) (level 3) (ai 1) (range 16) (sight 160 240) (exp 4) (itemgroup 2 2)
(str 27) (hth 2) (int 10) (wis 10) (dex 4) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
(attack 0 8  (magic 0 0) (defense 1 1) (absorb 0) (mspeed 1600 800) (resist 0 0 1 1 1)
(quest (2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)))
SECOND QUERY END -->

 

the thing is that the text is not in just 1 line each

after it start with "(npc " it is +3 lines long (all npc are like this) and + 1 empty line

the insert should be :

name,index..... VALUES 1,1

name,index..... VALUES 2,2

the row name should be take from the "(" start and value after the row name

at quest :

(quest (2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)))

should be

insert into npc (index,name,,,,quest) values (1,1,,, "(2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)")

 

hope you did understand ;)

thanks

Link to comment
Share on other sites

How about this for starters?

<?php
$string = "(npc (name 1) (index 1) (country 252) (race 0) (level 1) (ai 1) (range 20) (sight 160 240) (exp 2) (itemgroup 1 2)
(str 21) (hth 1) (int 10) (wis 10) (dex 2) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
(attack 0 7 7) (magic 0 0) (defense 0 0) (absorb 0) (mspeed 1600 800)
(quest (2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)))";

echo $string."<br/><br/>";

$string = substr($string,5,-2);
$string = str_replace(array("\r\n", "\n", "\r"),"%lb%",$string);

echo $string."<br/><br/>";

$stringExploded = explode("%lb%",$string);

var_dump($stringExploded);

$stringExploded["quest"] = substr($stringExploded[3],7);
unset($stringExploded[3]);

foreach  ($stringExploded as $key => $line) {
$line = substr($line,1,-1);
$stringExploded[$key] = explode(") (",$line);
}

var_dump($stringExploded);
?>

Link to comment
Share on other sites

try

<?php
$test = '(npc (name 1) (index 1) (country 252) (race 0) (level 1) (ai 1) (range 20) (sight 160 240) (exp 2) (itemgroup 1 2)
	(str 21) (hth 1) (int 10) (wis 10) (dex 2) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 7 7) (magic 0 0) (defense 0 0) (absorb 0) (mspeed 1600 800)
	(quest (2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)))

(npc (name 2) (index 2) (country 252) (race 0) (level 3) (ai 1) (range 16) (sight 160 240) (exp 4) (itemgroup 2 2)
	(str 27) (hth 2) (int 10) (wis 10) (dex 4) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 8  (magic 0 0) (defense 1 1) (absorb 0) (mspeed 1600 800) (resist 0 0 1 1 1)
	(quest (2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)))';

$test = preg_split('/\n\n/',$test);
foreach ($test as $line){
$line = trim($line);
// find table name
$start = 0;
for ($i = 0; $line[$i] !== ' '; $i++);
$table = substr($line,1, $i - 1);
// remove table name and last )
$lin = substr($line, $i + 1, strlen($line) - $i - 1);
$count = 0;
$key = array();
$value =array();
$part = '';
for ($i = 0; $i < strlen($lin); $i++){
	$chr = $lin[$i];
	if ($chr == '(') $count++;
	if ($chr == ')') $count--;
	if ($count > 0) {
		$part .= $chr;
	} elseif ($part) {
		$part = explode(' ', trim($part, '('), 2);
		$key[]= $part[0];
		$value[]= $part[1];
		$part = '';
	}
}
$key = '(`'.implode('`,`', $key).'`)';
$value = "('".implode("','",$value). "')";
$sql = 'INSERT INTO '. $table.' '. $key. ' VALUES '. $value;
echo $sql, "\n";//for testing
// do query
}
?>

Link to comment
Share on other sites

thank you sasa that did the work ;)

but it do make everything just in 1 query.. i need to make for every NPC a new query

 

INSERT INTO npc (`name`,`index`,`country`,`race`,`level`,`ai`,`range`,`sight`,`exp`,`itemgroup`,`str`,`hth`,`int`,`wis`,`dex`,`hp`,`mp`,`aspeed`,`hit`,`dodge`,`attack`,`magic`,`defense`,`absorb`,`mspeed`,`quest`,`name`,`index`,`country`,`race`,`level`,`ai`,`range`,`sight`,`exp`,`itemgroup`,`str`,`hth`,`int`,`wis`,`dex`,`hp`,`mp`,`aspeed`,`hit`,`dodge`,`attack`,`magic`,`defense`,`absorb`,`mspeed`,`resist`,`quest`) VALUES ('1','1','252','0','1','1','20','160 240','2','1 2','21','1','10','10','2','1','70','2400','0','0','0 7 7','0 0','0 0','0','1600 800','(2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)','2','2','252','0','3','1','16','160 240','4','2 2','27','2','10','10','4','1','70','2400','0','0','0 8 8','0 0','1 1','0','1600 800','0 0 1 1 1','(2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)') 

 

that is what it return

 

thanks again

Link to comment
Share on other sites

the code is returned as a 4 line output

INSERT INTO npc 

 

(`name`,`index`,`country`,`race`,`level`,`ai`,`range`,`sight`,`exp`,`itemgroup`,`str`,`hth`,`int`,`wis`,`dex`,`hp`,`mp`,`aspeed`,`hit`,`dodge`,`attack`,`magic`,`defense`,`absorb`,`mspeed`,`quest`,`name`,`index`,`country`,`race`,`level`,`ai`,`range`,`sight`,`exp`,`itemgroup`,`str`,`hth`,`int`,`wis`,`dex`,`hp`,`mp`,`aspeed`,`hit`,`dodge`,`attack`,`magic`,`defense`,`absorb`,`mspeed`,`resist`,`quest`)

 

 VALUES ('1','1','252','0','1','1','20','160 240','2','1 2','21','1','10','10','2','1','70','2400','0','0','0 7 7','0 0','0 0','0','1600 800','(2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933

 

 1)','2','2','252','0','3','1','16','160 240','4','2 2','27','2','10','10','4','1','70','2400','0','0','0 8 8','0 0','1 1','0','1600 800','0 0 1 1 1','(2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)') 

 

//edited the post

exactly how you see it return

Link to comment
Share on other sites

Mchl if you look carefull it continue from the third line ;)

 

Sasa,if i replace to your code the page is going to a continue loop,without nothing printing,just loading...

 

it was working till the maximum execution time reach..

now it return a query for every line =/

Link to comment
Share on other sites

 VALUES (
'1','1','252','0','1','1','20','160 240','2','1 2','21','1','10','10','2','1','70','2400','0','0','0 7 7','0 0','0 0','0','1600 800','(2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)',
'2','2','252','0','3','1','16','160 240','4','2 2','27','2','10','10','4','1','70','2400','0','0','0 8 8','0 0','1 1','0','1600 800','0 0 1 1 1','(2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)'
) 

 

That's a quote from previous post. I believe these are two records, and each should be enclosed in ().

Link to comment
Share on other sites

yes this is correct,sorry for misunderstooding you

this thing is harder than i expected  :(

 

//edit

 

here is what it return

INSERT INTO npc (`name`,`index`,`country`,`race`,`level`,`ai`,`range`,`sight`,`exp`,`itemgroup`) VALUES ('1','1','252','0','1','1','20','160 240','2','1 2') INSERT INTO str (``) VALUES ('') INSERT INTO attack (``) VALUES ('') INSERT INTO quest (`2`,`3`,`54`,`8001`) VALUES ('1 901 1','1 902 1','1 1025 1','1 933 1')
Fatal error: Maximum execution time of 60 seconds exceeded in LOCATION

Link to comment
Share on other sites

can you post exactly string for input

i try code for input

(npc (name 1) (index 1) (country 252) (race 0) (level 1) (ai 1) (range 20) (sight 160 240) (exp 2) (itemgroup 1 2)
	(str 21) (hth 1) (int 10) (wis 10) (dex 2) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 7 7) (magic 0 0) (defense 0 0) (absorb 0) (mspeed 1600 800)
	(quest (2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)))

(npc (name 2) (index 2) (country 252) (race 0) (level 3) (ai 1) (range 16) (sight 160 240) (exp 4) (itemgroup 2 2)
	(str 27) (hth 2) (int 10) (wis 10) (dex 4) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 8  (magic 0 0) (defense 1 1) (absorb 0) (mspeed 1600 800) (resist 0 0 1 1 1)
	(quest (2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)))

end it output is

INSERT INTO npc (`name`,`index`,`country`,`race`,`level`,`ai`,`range`,`sight`,`exp`,`itemgroup`,`str`,`hth`,`int`,`wis`,`dex`,`hp`,`mp`,`aspeed`,`hit`,`dodge`,`attack`,`magic`,`defense`,`absorb`,`mspeed`,`quest`) VALUES ('1','1','252','0','1','1','20','160 240','2','1 2','21','1','10','10','2','1','70','2400','0','0','0 7 7','0 0','0 0','0','1600 800','(2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)')
INSERT INTO npc (`name`,`index`,`country`,`race`,`level`,`ai`,`range`,`sight`,`exp`,`itemgroup`,`str`,`hth`,`int`,`wis`,`dex`,`hp`,`mp`,`aspeed`,`hit`,`dodge`,`attack`,`magic`,`defense`,`absorb`,`mspeed`,`resist`,`quest`) VALUES ('2','2','252','0','3','1','16','160 240','4','2 2','27','2','10','10','4','1','70','2400','0','0','0 8 8','0 0','1 1','0','1600 800','0 0 1 1 1','(2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)')

time is 0.037781 s

Link to comment
Share on other sites

sorry for double post,but i could not edit the last one

here is the current code

 

<?php
$test = '(npc (name 1) (index 1) (country 252) (race 0) (level 1) (ai 1) (range 20) (sight 160 240) (exp 2) (itemgroup 1 2)
	(str 21) (hth 1) (int 10) (wis 10) (dex 2) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 7 7) (magic 0 0) (defense 0 0) (absorb 0) (mspeed 1600 800)
	(quest (2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)))

(npc (name 2) (index 2) (country 252) (race 0) (level 3) (ai 1) (range 16) (sight 160 240) (exp 4) (itemgroup 2 2)
	(str 27) (hth 2) (int 10) (wis 10) (dex 4) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 8  (magic 0 0) (defense 1 1) (absorb 0) (mspeed 1600 800) (resist 0 0 1 1 1)
	(quest (2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)))';

$test = preg_split('/(\n|\r|\n\r){2,2}/',$test);
foreach ($test as $line){
$line = trim($line);
// find table name
$start = 0;
for ($i = 0; $line[$i] !== ' '; $i++);
$table = substr($line,1, $i - 1);
// remove table name and last )
$lin = substr($line, $i + 1, strlen($line) - $i - 1);
$count = 0;
$key = array();
$value =array();
$part = '';
for ($i = 0; $i < strlen($lin); $i++){
	$chr = $lin[$i];
	if ($chr == '(') $count++;
	if ($chr == ')') $count--;
	if ($count > 0) {
		$part .= $chr;
	} elseif ($part) {
		$part = explode(' ', trim($part, '('), 2);
		$key[]= $part[0];
		$value[]= $part[1];
		$part = '';
	}
}
$key = '(`'.implode('`,`', $key).'`)';
$value = "('".implode("','",$value). "')";
$sql = 'INSERT INTO '. $table.' '. $key. ' VALUES '. $value;
echo $sql, "\n";//for testing
// do query
}
?>

 

it do enter into maximum execution time and return a query for every line.

Link to comment
Share on other sites

it's somethin wrong with 1st split

can you try just first part of code

<?php
$test = '(npc (name 1) (index 1) (country 252) (race 0) (level 1) (ai 1) (range 20) (sight 160 240) (exp 2) (itemgroup 1 2)
	(str 21) (hth 1) (int 10) (wis 10) (dex 2) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 7 7) (magic 0 0) (defense 0 0) (absorb 0) (mspeed 1600 800)
	(quest (2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)))

(npc (name 2) (index 2) (country 252) (race 0) (level 3) (ai 1) (range 16) (sight 160 240) (exp 4) (itemgroup 2 2)
	(str 27) (hth 2) (int 10) (wis 10) (dex 4) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 8  (magic 0 0) (defense 1 1) (absorb 0) (mspeed 1600 800) (resist 0 0 1 1 1)
	(quest (2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)))';

$test = preg_split('/(\n|\r|\n\r){2,2}/',$test);
print_r($test);
?>

and post output

is it

Array
(
    [0] => (npc (name 1) (index 1) (country 252) (race 0) (level 1) (ai 1) (range 20) (sight 160 240) (exp 2) (itemgroup 1 2)
	(str 21) (hth 1) (int 10) (wis 10) (dex 2) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 7 7) (magic 0 0) (defense 0 0) (absorb 0) (mspeed 1600 800)
	(quest (2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)))
    [1] => (npc (name 2) (index 2) (country 252) (race 0) (level 3) (ai 1) (range 16) (sight 160 240) (exp 4) (itemgroup 2 2)
	(str 27) (hth 2) (int 10) (wis 10) (dex 4) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 8  (magic 0 0) (defense 1 1) (absorb 0) (mspeed 1600 800) (resist 0 0 1 1 1)
	(quest (2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)))
)

Link to comment
Share on other sites

here it is the problem ;)

 

Array ( [0] => (npc (name 1) (index 1) (country 252) (race 0) (level 1) (ai 1) (range 20) (sight 160 240) (exp 2) (itemgroup 1 2) [1] => (str 21) (hth 1) (int 10) (wis 10) (dex 2) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0) [2] => (attack 0 7 7) (magic 0 0) (defense 0 0) (absorb 0) (mspeed 1600 800) [3] => (quest (2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1))) [4] => [5] => (npc (name 2) (index 2) (country 252) (race 0) (level 3) (ai 1) (range 16) (sight 160 240) (exp 4) (itemgroup 2 2) [6] => (str 27) (hth 2) (int 10) (wis 10) (dex 4) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0) [7] => (attack 0 8  (magic 0 0) (defense 1 1) (absorb 0) (mspeed 1600 800) (resist 0 0 1 1 1) [8] => (quest (2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1))) ) 

 

here it to put every new line to array

Link to comment
Share on other sites

try

<?php
$test = '(npc (name 1) (index 1) (country 252) (race 0) (level 1) (ai 1) (range 20) (sight 160 240) (exp 2) (itemgroup 1 2)
	(str 21) (hth 1) (int 10) (wis 10) (dex 2) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 7 7) (magic 0 0) (defense 0 0) (absorb 0) (mspeed 1600 800)
	(quest (2 1 901 1) (3 1 902 1) (54 1 1025 1) (8001 1 933 1)))

(npc (name 2) (index 2) (country 252) (race 0) (level 3) (ai 1) (range 16) (sight 160 240) (exp 4) (itemgroup 2 2)
	(str 27) (hth 2) (int 10) (wis 10) (dex 4) (hp 1) (mp 70) (aspeed 2400) (hit 0) (dodge 0)
	(attack 0 8  (magic 0 0) (defense 1 1) (absorb 0) (mspeed 1600 800) (resist 0 0 1 1 1)
	(quest (2 1 901 1) (3 1 902 1) (55 1 1026 1) (8001 1 933 1)))';

$test = preg_split('/(\n|\r|\n\r){2,2}/',$test);
$test = array_chunk($test,5);
foreach ($test as $k => $v) $test[$k] = implode($v);
print_r($test);
?>

is it OK now

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.