Forum Moderators: coopster

Message Too Old, No Replies

Parsing Text in PHP

Parsing complex text...

         

alanv72

6:30 am on Aug 10, 2004 (gmt 0)

10+ Year Member


I have a string of text that represents a sketch drawing. Here is an example...

AACU50R88XR12D12XD38L34U8L32D8L34

the first "A" just means it the first sketch in this record. the next "A" means "start here". the c means put the pencil down and start drawing. it all goes from the bottom left around and ends up back at the starting point. so the U50 means draw a line going up 50 points. r88 means go 88 points right. xr12d12x means draw a diagnal line right12 spaces and down 12 spaces, d38 down 38, l34 left 34, u8 up 8, l32 left 32, d8 down 8, l34 left 34. and that brings you back to the starting point.

I'm try to parse this so I can turn this into a MING graphic. I have manually parsed this string and here is the PHP/MING code.
-------------------------------
<?php
//Sample sketch text: ACU50R88XR12D12XD38L34U8L32D8L34
//$SWF_SID = mt_rand();
//$SWF_FileName = 'RPID'.$SWF_SID.'.swf';
$font = new SWFFont("fonts/Bitstream Vera Sans.fdb"[smilestopper]);
$cap = new SWFText();
$cap->setFont($font);
$cap->setHeight(7);
$cap->setColor(0, 0, 0);

$sketch = new SWFShape();
//$sqfill = $square->addFill(0, 0, 0);
//$square->setRightFill($sqfill);
$sketch->setLine(1, 0, 0, 0);
$sketch->movePenTo(20,60);
$sketch->drawLineTo(20,10);
$cap->moveTo(9,37);
$cap->addString("50"[smilestopper]);
$sketch->drawLineTo(108,10);
$cap->moveTo(60,7);
$cap->addString("88"[smilestopper]);
$sketch->drawLineTo(120,22);
$sketch->drawLineTo(120,60);
$cap->moveTo(122,43);
$cap->addString("38"[smilestopper]);
$sketch->drawLineTo(86,60);
$cap->moveTo(99,68);
$cap->addString("34"[smilestopper]);
$sketch->drawLineTo(86,52);
$cap->moveTo(79,60);
$cap->addString("8"[smilestopper]);
$sketch->drawLineTo(54,52);
$cap->moveTo(66,60);
$cap->addString("32"[smilestopper]);
$sketch->drawLineTo(54,60);
$cap->moveTo(56,60);
$cap->addString("8"[smilestopper]);
$sketch->drawLineTo(20,60);
$cap->moveTo(33,68);
$cap->addString("34"[smilestopper]);
//$sketch->drawLineTo(-250,250);
//$sketch->drawLineTo(-250,-250);

//Next, we define the movie and place the movie clip (sprite) and the button in the movie itself:
$m = new SWFMovie();
$m->setDimension(140,80);
$m->setRate(12.0);
$m->add($sketch);
$m->add($cap);
$m->nextFrame();
//$i = $m->add($sketch);
//$i->setDepth(3);
//$i->moveTo(400, 300);
//$i->setName("box"[smilestopper]);

//Finally, we send the output to the browser:
//header("Pragma: private"[smilestopper]);
header("Content-Type: application/x-shockwave-flash"[smilestopper]);
$m->output();
?>
-----------------------------------

I've never parsed any text with PHP, and am having some trouble getting started. I'm thinking that I can break each trigger character into it's own function, but I'm a little lost on how to do that.

If anybody can help, I would greatly appreciate it.

Thanks,
alanv72

dcrombie

10:30 am on Aug 10, 2004 (gmt 0)



How's this:

$input = "AACU50R88XR12D12XD38L34U8L32D8L34"; 
preg_match_all("/X?[A-Z][0-9]*/", $input, $matches);

$matches[0] is then:

Array 
(
[0] => A
[1] => A
[2] => C
[3] => U50
[4] => R88
[5] => XR12
[6] => D12
[7] => XD38
[8] => L34
[9] => U8
[10] => L32
[11] => D8
[12] => L34
)