Skip to content

Instantly share code, notes, and snippets.

@rlorenzo
Created May 23, 2012 20:11
Show Gist options
  • Save rlorenzo/2777474 to your computer and use it in GitHub Desktop.
Save rlorenzo/2777474 to your computer and use it in GitHub Desktop.
UCLA catalog and section number formatters
/**
* Formats cat_num to display friendly version:
* 0000SSPP -> PP . int(0000) . SS
*
* @param string cat_num
*
* @return string Returns formatted string.
*/
function format_cat_num($cat_num)
{
$num = intval(substr($cat_num, 0, 4));
if (strlen($cat_num) < 5) {
$ss = ' ';
} else {
if (strlen($cat_num) < 6) {
$ss = $cat_num[4] . ' ';
} else {
$ss = $cat_num[4] . $cat_num[5];
}
}
if (strlen($cat_num) < 7) {
$pp = ' ';
} else {
if (strlen($cat_num) < 8) {
$pp = $cat_num[6] . ' ';
} else {
$pp = $cat_num[6] . $cat_num[7];
}
}
return trim(trim($pp) . $num . trim($ss));
}
/**
* Formats sec_num to display friendly version:
* P000SS -> P+CSTR(CINT(000))+SS //Trim accordingly
*
* @param string sec_num
*
* @param string Returns formatted string
*/
function format_sec_num($sec_num)
{
$secNum = '';
if (strlen($sec_num) <= 3) {
// if the string length is ~3, then just convert it to int
$secNum = intval($sec_num);
} elseif (strlen($sec_num) == 5) {
// then maybe the last character doesn't exist... truncated
$num = intval(substr($sec_num, 1, 3));
$SS = trim(substr($sec_num, 4, 1));
$secNum = $num . $SS;
} else {
// all characters should be present
$P = trim(substr($sec_num, 0, 1));
$num = intval(substr($sec_num, 1, 3));
$SS = trim(substr($sec_num, 4, 2));
$secNum = $P . $num . $SS;
}
return $secNum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment