Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
function reformat($data, $registration = false) {
    // Initializing aircompanies codes
    $iata = new iata;
    for ($i = 0; $i < count($data); $i = $i + 6) {
        if ($registration) {
            $r = str_replace('<nobr>', '', $this->prep_value($data[$i]));
            $block[$i]['reis'] = str_replace('</nobr>', '', $r);
            $a = explode(' ', $block[$i]['reis']);
            $block[$i]['company'] = $a[0];
        } else {
            $r = explode('&nbsp;', $this->prep_value($data[$i]));
            $block[$i]['reis'] = (strlen($r[0]) > 2) ? strrev(substr(strrev($r[0]), 15, 2)) . ' ' . $r[1] : $r[0] . ' ' . $r[1];
            $block[$i]['company'] = (strlen($r[0]) > 2) ? strrev(substr(strrev($r[0]), 15, 2)) : $r[0];
        }
        $block[$i]['companyname'] = $iata->aircompanies[strtoupper($block[$i]['company'])];
        if (file_exists(ROOT_HTML_PATH . '/content/aircompanies/' . $block[$i]['company'] . '.png')) {
            $block[$i]['company'] = $block[$i]['company'] . '.png';
        } else {
            /**
             * @todo Write e-mail notifications, but only once;
             */
        }
        $block[$i]['airport'] = $this->prep_value($data[$i + 1]);
        $block[$i]['terminal'] = substr(strrev($this->prep_value($data[$i + 2])), 2, 1);
        $block[$i]['shedtime'] = $this->prep_value($data[$i + 3]);
        $block[$i]['facttime'] = str_replace('&nbsp;', '', $this->prep_value($data[$i + 4]));
        $block[$i]['status'] = str_replace('&nbsp', '<br />', trim(strip_tags($this->prep_value($data[$i + 5]), '<img />'), '&nbsp;/r/n'));
        $block[$i]['status'] = str_replace(';', '', $block[$i]['status']);
    }
    return $block;
}
share|improve this question

1 Answer 1

up vote 1 down vote accepted
function reformat($data, $registration = false) {
    // Initializing aircompanies codes
    $iata = new iata;
    $block = array();
    for ($i = 0; $i < count($data); $i += 6) {
        $block[$i] = array(
            'airport'  => $this->prep_value($data[$i + 1]),
            'terminal' => substr(strrev($this->prep_value($data[$i + 2])), 2, 1),
            'shedtime' => $this->prep_value($data[$i + 3]),
            'facttime' => str_replace('&nbsp;', '', $this->prep_value($data[$i + 4])),
            'status'   => str_replace(';', '', 
                              str_replace('&nbsp', '<br />', 
                                  trim(
                                      strip_tags($this->prep_value($data[$i + 5]), '<img />'), 
                                      '&nbsp;/r/n'
                                  )
                              )
                          ),
        );

        if ($registration) {
            $r = str_replace('<nobr>', '', $this->prep_value($data[$i]));
            $block[$i]['reis'] = str_replace('</nobr>', '', $r);
            $a = explode(' ', trim($block[$i]['reis']));
            $block[$i]['company'] = $a[0];
        } else {
            $r = explode('&nbsp;', $this->prep_value($data[$i]));
            $block[$i]['company'] = (strlen($r[0]) > 2) ? substr($r[0], -17, 2) : $r[0];
            $block[$i]['reis'] = $block[$i]['company'] . ' ' . $r[1];
        }
        $block[$i]['companyname'] = $iata->aircompanies[strtoupper($block[$i]['company'])];
        if (file_exists(ROOT_HTML_PATH . '/content/aircompanies/' . $block[$i]['company'] . '.png')) {
            $block[$i]['company'] = $block[$i]['company'] . '.png';
        } else {
            /**
             * @todo Write e-mail notifications, but only once;
             */
        }
    }
    return $block;
}

But it is many questions here:

$this->prep_value($data[$i + 2])

It is really bad idea. May be you need a little another cycle, like

for ($i = 0; $i < count($data) - 6; $i ++) { // or something for preventing array index overloading

It would be much better. But I cannot know it for sure.

share|improve this answer
    
$this->prep_value($data[$i + 2]) That's cause of parsing data. Sorry, could do nothing with this. Maybe =) for ($i = 0; $i < count($data) - 6; $i ++) That's cause i use for block[0] - first six items, for block[1] - next six items. Really, don't know what to do with this. –  plisovyi Apr 13 '11 at 13:35
    
It appears that your data comes in as an array $data. What you could consider based on what is in that data array, is to perform all str_replace operations in one go at the top (if feasible). You could flatten the array initially with json_encode then do str_replace on all the <br/>, /r/n, $nbsp etc. and then recreate it with json_decode. That could speed the process up. Again it depends on your data. –  Nikolaos Dimopoulos Jun 3 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.