» Programming » PHP » String Functions

arrtoupper() and arrtolower()


Make all string within an array upper case or lower case. These are the same as strtoupper() and strtolower() in PHP, except they are performed on every element of an array (recursively).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function arrtoupper($arr) {
  if (!is_array($arr)) {
    $arr = strtoupper($arr);
  } else {
    foreach ($arr as $key => $val) $arr[$key] = arrtoupper($val);
  }
  return $arr;
}
 
 
function arrtolower($arr) {
  if (!is_array($arr)) {
    $arr = strtolower($arr);
  } else {
    foreach ($arr as $key => $val) $arr[$key] = arrtolower($val);
  }
  return $arr;
}

 
 

Clean Phone Numbers


This will convert all of the letters in phone numbers into actual phone numbers, as well as optionally strip out all non-numeric characters (such as plus signs, hyphens, etc)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function clean_phone_number($number, $allow_non_number=false) {
  $pattern = array('/[abc]/is', '/[def]/is',  '/[ghi]/is', '/[jkl]/is',
                   '/[mno]/is', '/[pqrs]/is', '/[tuv]/is', '/[wxyz]/is');
 
  $replace = array('2',         '3',          '4',         '5',
                   '6',         '7',          '8',         '9');
 
  if ($allow_non_number !== true) {
    $pattern[] = '/[^0-9]/';
    $replace[] = '';
  }
 
  return preg_replace($pattern, $replace, $number);
}

 
 

Cleanly Formated File Sizes


These functions will convert to/from cleanly formatting string and numerical values.

bytes_to_string(1048576) will return “1 MiB
string_to_bytes(“1G”) will return “1073741824

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
define('BYTE_FORMAT_K',     0x0001);
define('BYTE_FORMAT_KB',    0x0002);
define('BYTE_FORMAT_KIB',   0x0003);
 
function bytesToString($value, $format=false, $sep=' ') {
  if ($format === false) $format = BYTE_FORMAT_KIB;
 
  switch ($format) {
    case BYTE_FORMAT_K:
      $units = array('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
      $size  = 1024;
    break;
 
    case BYTE_FORMAT_KB:
      $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
      $size  = 1000;
    break;
 
    case BYTE_FORMAT_KIB:
      $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
      $size  = 1024;
    break;
 
    default:
      return false;
  }
 
  for ($i=0; $i<count($units); $i++) {
    if ( ($value >= pow($size, $i) )  &&  ($value <= pow($size, $i+1)) ) {
      return maxDigits( ($value / pow($size, $i)), 4 ) . $sep . $units[$i];
    }
  }
  return false;
}
 
 
function stringToBytes($string) {
  $value = (float)trim($string);
  $rest  = trim(substr($string, strlen((string)$value)));
  switch (strtoupper($rest)) {  //TODO: strip out all non-alpha characters!
    case 'B': return round($value);
    case 'K': case 'KIB': return round($value * pow(1024, 1));
    case 'M': case 'MIB': return round($value * pow(1024, 2));
    case 'G': case 'GIB': return round($value * pow(1024, 3));
    case 'T': case 'TIB': return round($value * pow(1024, 4));
    case 'P': case 'PIB': return round($value * pow(1024, 5));
    case 'E': case 'EIB': return round($value * pow(1024, 6));
    case 'Z': case 'ZIB': return round($value * pow(1024, 7));
    case 'Y': case 'YIB': return round($value * pow(1024, 8));
    case 'KB': return round($value * pow(1000, 1));
    case 'MB': return round($value * pow(1000, 2));
    case 'GB': return round($value * pow(1000, 3));
    case 'TB': return round($value * pow(1000, 4));
    case 'PB': return round($value * pow(1000, 5));
    case 'EB': return round($value * pow(1000, 6));
    case 'ZB': return round($value * pow(1000, 7));
    case 'YB': return round($value * pow(1000, 8));
  }
  return 0;
}
 
function maxDigits($value, $digits) {
  return round($value, max(0, $digits-strlen((string)round($value))));
}

 
 

Obfuscate Email Address


This function will return an obfuscated version of an email address to make it harder for bots to pick up on them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function encode_email_address($address) {
  $final = '';
 
  $len = strlen($address);
  for ($i=0; $i<$len; $i++) {
    if (rand(0,1)) {
      $final .= '&#x' . dechex(ord(substr($address, $i, 1))) . ';';
    } else {
      $final .= '&#' . ord(substr($address, $i, 1)) . ';';
    }
  }
 
  return $final;
}