Jump to content

Count X and Y from a file


Fricti0n

Recommended Posts

Hi, I'm wondering if someone know how to do this? =]

 

So, I have this inside a XML file

1                        0
1 xx  xx  xx  xx  xx  xx 0
1 xx  xx  xx  xx  xx  xx 0
1 xx  xx  xx  xx  xx  xx 0
1 xx  xx  xx  xx  xx  xx 0
1 xx  xx  xx  xx  xx  xx 0
1 xx  xx  xx  xx  xx  xx 0
1 xx  xx  xx  xx  xx  xx 0
1 xx  xx  xx  xx  xx  xx 0
1 xx  xx  xx  xx  xx  xx 0
1 xx  xx  xx  xx  xx  xx 0
1                        0

 

That is parsed through this function in PHP

function sm_str_replace($search, $replace, $subject) {
    return strtr( $subject, array_combine($search, $replace) );
}
$xmlSeatmap = 'modules/seatmap/seatmap.tg11.'.$_GET['zone'].'.xml'; // Defines witch file that should be extracted

$countData = file_get_contents($xmlSeatmap);
$charCount = strlen($countData);

$openSeatmap = fopen($xmlSeatmap, 'r');
$xmlData = fread($openSeatmap, $charCount);
fclose($openSeatmap);

$search = array('1', '0', ' ', 'x', 's', 'g', 'c', 't');
$replace = array('<tr>', '</tr>', '<td></td>', '<td class="seatmapAvailable" rel="tooltip" alt="Dette setet er ledig (Rad 3 Sete 2)"></td>', '<td class="seatmapScene"></td>', '<td class="seatmapGame"></td>', '<td class="seatmapCreative"></td>', '<td class="seatmapTaken"></td>');

echo '<table class="seatmapContainer">';
echo sm_str_replace($search, $replace, $xmlData);
echo '</table>';

 

It is possible to count X and Y in some way so the first vertical line with X's would be ROW_1 and the second vertical line would be ROW_2, and so on?

(The second vertical line is the X's that are on the side with The first vertical line)

 

Thanks for any help! =]

rCon^

Link to comment
https://forums.phpfreaks.com/topic/198811-count-x-and-y-from-a-file/
Share on other sites

Do it line by line:

http://php.net/manual/en/function.fgets.php

 

so you would surround your parsing with:

while (!eof($openSeatMap)){

then you could read one line

$line=fgets($openSeatMap);

do the replace, and perform any mathematics (such as incrementing a counter)

Any examples on how to do that?

http://php.net/manual/en/function.fgets.php did not give me any help or ideas...

 

I know that for is needed:

There is 12 lines in the xml file, so $i = 12

for($i = 12; $i >= 1; $i--) {

}

 

It is possible to have a replace array inside a for loop, and decrease the seat number from 10 to 1?

the seat on the top is 10, and on the bottom is 1..

 

rCon^

  • 2 weeks later...

You want to count the number of x's and y's in the file?

 

All you need to do (if its usually that small a file) is to use:

$file = implode("\n",file("test.xml"));?>

 

Then uou can use preg_replace and strlen to count the difference when you replace the character you want to check.

$xnum = strlen($file) - strlen(preg_replace("/x/i","",$file));
$ynum = strlen($file) - strlen(preg_replace("/y/i","",$file));

 

-cb-

  • 1 month later...

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.