Merubah format teks menjadi angka dan sebaliknya dengan PHP

Bismillaahirrohmaanirrohiim…

Sebenarnya merubah text format menjadi angka dan sebaliknya merubah format angka menjadi teks disini adalah kebutuhan saya sendiri.

Biar tidak lupa, maka saya posting diweb rasupe.

Yang pertama, membuat teks format.

Misalkan kita ingin membuat format kartu kredit atau nomor HP dengan sumber asal teks saja.

Contoh:

$text = 'abcdefghij';
menjadi
$fmt_text = 'ab-cd-ef-ghij';

Pada contoh di atas, format text terdiri dari digit 2,2,2,4.
Berikut fungsinya:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function textToFormatted($text, $separator='-', $formats = array(2,2,2,4)) {
//abcdefghij to ab-cd-ef-ghij
$sum_formats = array_sum($formats);
//wrong format
if ($sum_formats !== strlen($text)) return '';
$num_formats = count($formats);
$fmt_text = '';
$start = 0;
for ($i=0; $i<$num_formats; $i++) {
$digit = $formats[$i];
if ($i > 0) $fmt_text .= $separator;
$fmt_text .= substr($text, $start, $digit);
$start += $digit;
}
return $fmt_text;
}
function textToFormatted($text, $separator='-', $formats = array(2,2,2,4)) { //abcdefghij to ab-cd-ef-ghij $sum_formats = array_sum($formats); //wrong format if ($sum_formats !== strlen($text)) return ''; $num_formats = count($formats); $fmt_text = ''; $start = 0; for ($i=0; $i<$num_formats; $i++) { $digit = $formats[$i]; if ($i > 0) $fmt_text .= $separator; $fmt_text .= substr($text, $start, $digit); $start += $digit; } return $fmt_text; }
function textToFormatted($text, $separator='-', $formats = array(2,2,2,4)) {
  //abcdefghij to ab-cd-ef-ghij
  $sum_formats = array_sum($formats);

  //wrong format
  if ($sum_formats !== strlen($text)) return '';

  $num_formats = count($formats);

  $fmt_text = '';
  $start = 0;
  for ($i=0; $i<$num_formats; $i++) {
    $digit = $formats[$i];

    if ($i > 0) $fmt_text .= $separator;

    $fmt_text .= substr($text, $start, $digit);

    $start += $digit;
  }

  return $fmt_text;
}
Yang kedua, membuat format angka menjadi abjad.

Contoh:
$text = '12-34-56-789';
menjadi
$fmt_text = 'bc-de-fg-ahij';

Pada contoh di atas, $fmt_text terdiri dari digit 2,2,2,4. Meski kode sumbernya 2,2,2,3.
Jika kode sumber berbeda maka digit pertama dari 789 ditambah  (0789) sehingga hasil convertnya menjadi ahij.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function numberFormatToText($fmt_number, $separator='-', $formats = array(2,2,2,4)) {
//12-34-56-789 to bc-de-fg-ahij
//digit harus sama persis, jika beda maka dimulai dari 0
$text = '';
$abjad = range('a', 'j');//0-9
$exp = explode($separator, $fmt_number);
$num_formats = count($formats);
$num_exp = count($exp);
//wrong format
if ($num_formats!==$num_exp) return '';
for ($i=0; $i<$num_formats; $i++) {
$digit = $formats[$i];
//wrong format
if (empty($digit)) return '';
$text_part = $exp[$i];
$text_part = preg_replace("/[^0-9]/","",$text_part);
if (empty($text_part)) $text_part = '0';
$text_length = strlen($text_part);
if ($text_length < $digit) {
$repeat = str_repeat('0', $digit-$text_length);
$text_part = $repeat.$text_part;
}
if ($text_length > $digit) {
$text_part = substr($text_part, 0, $digit);
}
if ($i > 0) $text .= $separator;
$split = str_split($text_part);
foreach ($split as $number) {
$text .= $abjad[$number];
}
}
return $text;
}
function numberFormatToText($fmt_number, $separator='-', $formats = array(2,2,2,4)) { //12-34-56-789 to bc-de-fg-ahij //digit harus sama persis, jika beda maka dimulai dari 0 $text = ''; $abjad = range('a', 'j');//0-9 $exp = explode($separator, $fmt_number); $num_formats = count($formats); $num_exp = count($exp); //wrong format if ($num_formats!==$num_exp) return ''; for ($i=0; $i<$num_formats; $i++) { $digit = $formats[$i]; //wrong format if (empty($digit)) return ''; $text_part = $exp[$i]; $text_part = preg_replace("/[^0-9]/","",$text_part); if (empty($text_part)) $text_part = '0'; $text_length = strlen($text_part); if ($text_length < $digit) { $repeat = str_repeat('0', $digit-$text_length); $text_part = $repeat.$text_part; } if ($text_length > $digit) { $text_part = substr($text_part, 0, $digit); } if ($i > 0) $text .= $separator; $split = str_split($text_part); foreach ($split as $number) { $text .= $abjad[$number]; } } return $text; }
function numberFormatToText($fmt_number, $separator='-', $formats = array(2,2,2,4)) {
  //12-34-56-789 to bc-de-fg-ahij
  //digit harus sama persis, jika beda maka dimulai dari 0
  $text  = '';
  $abjad = range('a', 'j');//0-9

  $exp = explode($separator, $fmt_number);

  $num_formats = count($formats);
  $num_exp = count($exp);

  //wrong format
  if ($num_formats!==$num_exp) return '';

  for ($i=0; $i<$num_formats; $i++) {
    $digit = $formats[$i];

    //wrong format
    if (empty($digit)) return '';

    $text_part = $exp[$i];
    $text_part = preg_replace("/[^0-9]/","",$text_part);
    if (empty($text_part)) $text_part = '0';

    $text_length = strlen($text_part);
    if ($text_length < $digit) {
      $repeat = str_repeat('0', $digit-$text_length);
      $text_part = $repeat.$text_part;
    }
    if ($text_length > $digit) {
      $text_part = substr($text_part, 0, $digit);
    }

    if ($i > 0) $text .= $separator;

    $split = str_split($text_part);
    foreach ($split as $number) {
      $text .= $abjad[$number];
    }

  }

  return $text;
}
Yang ketiga, membuat format text menjadi angka.

Contoh:

$text = 'ab-cd-ef-ghij';
menjadi
$fmt_text = '01-23-45-6789';

Pada contoh di atas, format text terdiri dari digit 2,2,2,4.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function textFormatToNumber($fmt_text, $separator='-', $formats = array(2,2,2,4)) {
//ab-cd-ef-ghij to 01-23-45-6789
//digit harus sama persis, jika beda maka dimulai dari a
$text = '';
$numbers = array_flip(range('a', 'j'));//0-9
$exp = explode($separator, $fmt_text);
$num_formats = count($formats);
$num_exp = count($exp);
//wrong format
if ($num_formats!==$num_exp) return '';
for ($i=0; $i<$num_formats; $i++) {
$digit = $formats[$i];
//wrong format
if (empty($digit)) return '';
$text_part = strtolower($exp[$i]);
$text_part = preg_replace("/[^a-z]/","",$text_part);
if (empty($text_part)) $text_part = 'a';
$text_length = strlen($text_part);
if ($text_length < $digit) {
$repeat = str_repeat('a', $digit-$text_length);
$text_part = $repeat.$text_part;
}
if ($text_length > $digit) {
$text_part = substr($text_part, 0, $digit);
}
if ($i > 0) $text .= $separator;
$split = str_split($text_part);
foreach ($split as $abjad) {
if (isset($numbers[$abjad])) {
$text .= $numbers[$abjad];
} else {
$text .= $abjad;
}
}
}
return $text;
}
function textFormatToNumber($fmt_text, $separator='-', $formats = array(2,2,2,4)) { //ab-cd-ef-ghij to 01-23-45-6789 //digit harus sama persis, jika beda maka dimulai dari a $text = ''; $numbers = array_flip(range('a', 'j'));//0-9 $exp = explode($separator, $fmt_text); $num_formats = count($formats); $num_exp = count($exp); //wrong format if ($num_formats!==$num_exp) return ''; for ($i=0; $i<$num_formats; $i++) { $digit = $formats[$i]; //wrong format if (empty($digit)) return ''; $text_part = strtolower($exp[$i]); $text_part = preg_replace("/[^a-z]/","",$text_part); if (empty($text_part)) $text_part = 'a'; $text_length = strlen($text_part); if ($text_length < $digit) { $repeat = str_repeat('a', $digit-$text_length); $text_part = $repeat.$text_part; } if ($text_length > $digit) { $text_part = substr($text_part, 0, $digit); } if ($i > 0) $text .= $separator; $split = str_split($text_part); foreach ($split as $abjad) { if (isset($numbers[$abjad])) { $text .= $numbers[$abjad]; } else { $text .= $abjad; } } } return $text; }
function textFormatToNumber($fmt_text, $separator='-', $formats = array(2,2,2,4)) {
  //ab-cd-ef-ghij to 01-23-45-6789
  //digit harus sama persis, jika beda maka dimulai dari a
  $text  = '';
  $numbers = array_flip(range('a', 'j'));//0-9

  $exp = explode($separator, $fmt_text);

  $num_formats = count($formats);
  $num_exp = count($exp);

  //wrong format
  if ($num_formats!==$num_exp) return '';

  for ($i=0; $i<$num_formats; $i++) {
    $digit = $formats[$i];

    //wrong format
    if (empty($digit)) return '';

    $text_part = strtolower($exp[$i]);
    $text_part = preg_replace("/[^a-z]/","",$text_part);
    if (empty($text_part)) $text_part = 'a';

    $text_length = strlen($text_part);
    if ($text_length < $digit) {
      $repeat = str_repeat('a', $digit-$text_length);
      $text_part = $repeat.$text_part;
    }
    if ($text_length > $digit) {
      $text_part = substr($text_part, 0, $digit);
    }

    if ($i > 0) $text .= $separator;

    $split = str_split($text_part);
    foreach ($split as $abjad) {
      if (isset($numbers[$abjad])) {
        $text .= $numbers[$abjad];
      } else {
        $text .= $abjad;
      }
    }

  }

  return $text;
}

Fungsi PHP di atas sengaja dibuat dinamis, sehingga separator dan digit bisa dirubah sesuai kebutuhan.

Baca Juga:   Daftar timezone PHP lengkap seluruh dunia

ika ingin merubah separator, tinggal dirubah pada saat memanggil fungsinya, bisa menggunakan “=”, “|”, “/”, dll
Digit hasil format juga dapat dirubah sesuai keinginan, misal 3,4,5 atau 1,5,7, dll

Nah, begitu dulu tutorial kali ini, cara ini bisa dikembangkan dan digunakan untuk hal-hal yang lain.

Semoga bermanfaat.

Bagikan

You May Also Like

About the Author: rasupe

Leave a Reply

Discover more from Rasupe

Subscribe now to keep reading and get access to the full archive.

Continue reading