0

I am working in Youtube downloader project. Everything works fine except when I am trying to put these two portion of code in different place. I think for the usage of same variable name with different value in one script

    $i = 0;
    $ipbits = $ip = $itag = $sig = $size = '';

this problem occurs. I can't change the variable name because this variable name will be assigned by parse_str function in both adaptipe_array and stream_map_array with some different value. Currrently my scripts works but I think its not a good programming practice. Any suggestion for alternative to this or can it be modified in another way? Help will be appreciated.

/* create an array of available download formats */

    $avail_formats[] = '';
    $i = 0;
    $ipbits = $ip = $itag = $sig = $size = '';
    $expire = time(); 

    foreach($adaptive_array as $format) {
        parse_str($format);
        $avail_formats[$i]['itag'] = $itag;
        $avail_formats[$i]['size'] = $size;
        $type = explode(';',$type);
        $avail_formats[$i]['type'] = $type[0];
        $avail_formats[$i]['url'] = urldecode($url) . '&signature=' . $sig;
        parse_str(urldecode($url));
        $avail_formats[$i]['expires'] = date("G:i:s T", $expire);
        $avail_formats[$i]['ipbits'] = $ipbits;
        $avail_formats[$i]['ip'] = $ip;
        $i++;

    }



    /* create an array of available download formats */

    $avail_formats2[] = '';
    $i = 0;
    $ipbits = $ip = $itag = $sig = $quality = '';
    $expire = time(); 

    foreach($stream_map_array as $format) {
        parse_str($format);
        $avail_formats2[$i]['itag'] = $itag;
        $avail_formats2[$i]['quality'] = $quality;
        $type = explode(';',$type);
        $avail_formats2[$i]['type'] = $type[0];
        $avail_formats2[$i]['url'] = urldecode($url) . '&signature=' . $sig;
        parse_str(urldecode($url));
        $avail_formats2[$i]['expires'] = date("G:i:s T", $expire);
        $avail_formats2[$i]['ipbits'] = $ipbits;
        $avail_formats2[$i]['ip'] = $ip;
        $i++;
    }
2
  • What does this line mean? "I think for the usage of same variable name with different value in one script" Commented Jul 18, 2014 at 7:03
  • thats mean i am using same variable name in two place for assiging different value at same time and i need the same variable name for parse_string function @RishiDua Commented Jul 18, 2014 at 7:18

2 Answers 2

3

If both code portions are performing almot the same computation on different arrays, you can turn them into one function. The differences may be factored out as parameters for the function:

* create an array of available download formats */
function make_available_format($formats,$ename){ 
  $avail_formats[] = '';
  $i = 0;
  $ipbits = $ip = $itag = $sig = $extra = '';
  $expire = time(); 

  foreach($formats as $format) {
    parse_str($format);
    $avail_formats[$i]['itag'] = $itag;
    $avail_formats[$i][$ename] = $extra;
    $type = explode(';',$type);
    $avail_formats[$i]['type'] = $type[0];
    $avail_formats[$i]['url'] = urldecode($url) . '&signature=' . $sig;
    parse_str(urldecode($url));
    $avail_formats[$i]['expires'] = date("G:i:s T", $expire);
    $avail_formats[$i]['ipbits'] = $ipbits;
    $avail_formats[$i]['ip'] = $ip;
    $i++;
  }
  return $avail_formats;
}

Then use it as follow:

$avail_formats = make_available_format($adaptive_array,'size');
$avail_formats2 = make_available_format($stream_map_array,'quality')
);

// or maybe
$avail_formats = array(
    make_available_format($adaptive_array,'size'),
    make_available_format($stream_map_array,'quality')
);

You could also externalize the value of $extra if needed.

Sign up to request clarification or add additional context in comments.

Comments

1

Use functions.

function get_formats($input_array) {
   $avail_formats = array();
   $ipbits = $ip = $itag = $sig = $quality = '';
   $expire = time(); 

   foreach($input_array as $format) {
       $entry = array();
       parse_str($format);
       $entry['itag'] = $itag;
       $entry['quality'] = $quality;
       $type = explode(';',$type);
       $entry['type'] = $type[0];
       $entry['url'] = urldecode($url) . '&signature=' . $sig;
       parse_str(urldecode($url));
       $entry['expires'] = date("G:i:s T", $expire);
       $entry['ipbits'] = $ipbits;
       $entry['ip'] = $ip;
       $avail_formats[] = $entry;
   }
   return $avail_formats;
}


$avail_formats = get_formats($adaptive_array);
$avail_formats2 = get_formats($stream_map_array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.