Thursday, 4 June 2015

New functions for PHP3

 For those that know Perl, you will recognize
a lot of these.All but one deal with array handling. Sorting, splicing, etc.

OK, I finally got around to putting these all together. I was
originally creating a class to handle arrays, but functions
are easier to remember and call.

<?php
ADD-INS FOR PHP3
#
# These functions should look familiar to anyone that knows
# Perl. I use them all the time so I made these rather than
# have to code them over and over. There are also some
# functions that do not relate to Perl.
#
# Function list:
#
# Chomp
# Shift
# Unshift
# Pop
# Push
# Splice
# Sort unique
# Truncate array
# Get array range



Chomp function
#
# Removes trailing line breaks from a given string
# If 'int' is specified it removes int line breaks
# which is much faster than calling chomp several
# times.
#

  function chomp(&$string, $int = 1){
    if (!$string || $int < 1){ return; }
    for ($x = 0; $x < $int; $x++){
      if (ereg("\n$",$string,$ref)){
        $string = ereg_replace("\n$","",$string);
      }else{
        return;
      }
    }
    unset($x);
    unset($int);
  }

#



Shift function
#
# Shifts array from left to right. This will shift off the
# zeroeth (first) element from the array and move all other
# elements to their new positions, shifted 1 space left.
#

  function shift(&$array){
    if (!is_array($array) || !is_string($variable)){ return; }
    $variable = $array[0];
    for ($x = 1; $x < count($array); $x++){
      $temp[count($temp)] = $array[$x];
    }
    $array = $temp;
    unset($temp);
    return $variable;
  }

#



Unshift function
#
# This is the reverse of the shift function in that it will
# add $variable to the front of $array. The number of
# elements in the new array is returned.
#

  function unshift(&$array,$variable){
    if (!is_array($array)){ return; }
    if (!is_array($variable)){
      $temp = $array;
      unset($array);
      $array[0] = $variable;
      for ($x = 0; $x < count($temp); $x++){
        $array[count($array)] = $temp[$x];
      }
      unset($temp);
      unset($x);
    }elseif(is_array($variable)){      
      $temp = $array;
      unset($array);
      for ($x = 0; $x < count($variable); $x++){
        $array[count($array)] = $variable[$x];
      }
      for ($x = 0; $x < count($temp); $x++){
        $array[count($array)] = $temp[$x];
      }
      unset($temp);
      unset($x);
    }
    return count($array);
  }

#



Pop function
#
# 'Pops' off and returns the last element of $array. By
# doing so, the size of $array is reduced by 1.
#

  function pop(&$array){
    if (!is_array($array)){ return; }
    $variable = $array[count($array)-1];
    unset($array[count($array)-1]);
    return $variable;
  }

#



Push function
#
# 'Pushes' $variable onto $array where $variable becomes
# the last element in $array. If $variable is an array
# then all elements of $variable are added to $array and
# $array will increase in size equal to the number of
# elements that were in $variable. The new size of $array
# is returned from this function.
#

  function push(&$array,$variable){
    if (!is_array($array)){ return; }
    if (!is_array($variable)){
      $array[count($array)] = $variable;
    }elseif (is_array($variable)){
      for ($x = 0; $x < count($variable); $x++){
        $array[count($array)] = $variable[$x];
      }
    }
    unset($x);
    return count($array);
  }

#



Splice function
#
# This function lets you add elements to the beginning,
# middle, or end of an array. You can replace portions
# of an array with other arrays, or elements. If any
# variables were replaced in $array they are returned.
#

  function splice(&$array,$skip,$length,$variable){
    if (!is_array($array) || $length == 0){ return; }
    $replaced = array();
    if ($skip < 0){
      if (!is_array($variable)){
        $array[count($array)] = $variable;
      }elseif (is_array($variable)){
        for ($x = 0; $x < count($variable); $x++){
          $array[count($array)] = $variable[$x];
        }
      }
      return $replaced;
    }

    for ($x = 0; $x < count($array); $x++){
      if ($x >= $skip){
        if (!is_array($variable)){
          if ($length > 1){

/* Disabled splicing of arrays since I can't figure how
   to do it right now. Will have to look at this later.
   The splicing of single variables still works fine.


            for ($y = $skip; $y < ($length+$skip); $y++){
              if ($y < count($array)){
                $replaced[count($replaced)] = $array[$y];
              }
            }

            $array[$skip] = $variable;



            break;
*/

          }else{
            $replaced[count($replaced)] = $array[$x];
            $array[$x] = $variable;
            break;
          }


        }elseif (is_array($variable)){
          for ($y = $skip; $y <= $length; $y++){
            $replaced[count($replaced)] = $array[$y];
            $array[$y] = $array[($skip+$length)];
          }
          for ($y = (count($array)-$length); $y < count($array); $y++){
            unset($array[$y]);
          }
          $temp = $array;
          unset($array);
          for ($y = ($skip-1); $y >= 0; $y--){
            $array[$y] = $temp[$y];
          }
          for ($y = $skip; $y < count($variable); $y++){
            $array[count($array)] = $variable[$y];
          }
          for ($y = $skip; $y < count($temp); $y++){
            $array[count($array)] = $temp[$y];
          }
        }
      }
    }
    return $replaced;
  }

#



Sort unique functions
#
# Sorts an array into unique elements. If there are arrays
# within the top level array, only the second level arrays
# are sorted. Any arrays beyond the second level are not sorted.
# if $type is specified as 'nocase' then all elements are
# sorted case-insensitive.
#

  function sort_unique(&$data, $type = ""){
    if (!is_array($data)){
      settype($data,"array");
    }
    if (count($data) < 1){
      print "Array is empty!<br>\n";
      return;
    }
    if (isset($temp)){ unset($temp); }

    $count = 0;
    for ($x = 0; $x < count($data); $x++){
      if (is_array($data[$x])){
        sort_unique_sub(&$data[$x], $type);
        $count++;
      }
    }

    if ($count == 0){
      sort_unique_sub(&$data, $type);
    }
  }

  function sort_unique_sub ($data, $type = ""){
    if (isset($temp)){ unset($temp); }
    if ($type == "nocase"){
      for ($x = 0; $x < count($data); $x++){
        $data[$x] = strtolower($data[$x]);
      }
    }
    sort($data);
    for ($x = 0; $x < count($data); $x++){
      if ($data[$x] != $data[$x+1]){
        $temp[sizeof($temp)] = $data[$x];
      }
    }
    unset ($data);
    for ($x = 0; $x < count($temp); $x++){
      $data[$x] = $temp[$x];
    }
  }

#



Truncate array function
#
# This function will remove all elements from the specified
# element to the end of array.
#

  function truncate_array(&$data, $element){
    if (!is_array($data)){
      settype($data,"array");
    }
    if (count($data) < 1){
      print "Array is empty!<br>\n";
      return;
    }
    if (!$element){
      print "Must specify array element!<br>\n";
      return;
    }
    if (count($data) < $element){
      $num = count($data);
      print "Array only has $num elements!<br>\n";
      return;
    }

    for ($x = count($data); $x >= 1; $x--){
      if ($x >= $element){
        unset($data[$x-1]);
      }
    }
  }

#



Get array range function
#
# This function will take an array and a range of elements. It
# then alters the array so that only the range of elements
# specified exist in the array.
#

  function get_array_range(&$data, $range = ""){
    if (!is_array($data)){
      settype($data,"array");
    }
    if (count($data) < 1){
      print "Array is empty!<br>\n";
      return;
    }
    if (!$range){
      print "No range specified!<br>\n";
      return;
    }

    list($start,$end) = split("-",$range);
    if ($start > $end){
      $temp = $start;
      $start = $end;
      $end = $temp;
    }

    if ($end > count($data)){
      $num = count($data);
      print "Array only has $num elements!<br>\n";
      return;
    }

    if (isset($temp)){ unset($temp); }

    for ($x = 1; $x <= count($data); $x++){
      if ($x >= $start && $x <= $end){
        $temp[sizeof($temp)] = $data[$x-1];
      }
    }
    unset($data);
    $data = $temp;
    unset($temp);

  }

#

?>

0 comments:

Post a Comment