Location: PHPKode > scripts > Mklib > mklib.phps
<?
/*
    mklib.php ver 0.4.3
    "putting the fun back in function"

    blazonry.com
    Created On: ??


-----------------------------------------------------------------------
** CHANGES
-----------------------------------------------------------------------
    ver 0.4.3  2001-08-22
        * added convert_array_to_options()
    
    ver 0.4.2  2001-02-07
        * moved date/time functions to mk_datetime.php


    ver 0.4.1   2001-01-19
        * added underscore to valid URL character in procDisplayText


    ver 0.4.0   2000-12-12
        * changed mk(.*) to mk_(.*)
        * added mk_ fn's for above backwards compatibility
        * added ellipses to mk_substr()
        * added back format_bytes

    ver 0.3.9   2000-12-08
        * added mk_getVersion()
        * changed mksplit to use explode (deprecated)
        * changed procDate y2k tweak to 70

    ver 0.3.8   2000-12-03
        * added mkerror()

    ver 0.3.7   2000-11-04
        * formatted library consistently
        * started to add javadoc type documentation for each function


        
*/

/*  ===================================================================
 * MK Simplified License:
 *
 *  By using my software you agree to the following
 *  two terms:
 *      1. You won't take credit for my work
 *      2. You won't profit from my work
 *
 *  ====================================================================
 */

function mk_getVersion()
{
    return "mklib.php ver 0.4.3";
}



//---------------------------------------------------------------------
// ** File FUNCTIONS
// --------------------------------------------------------------------

/**
 * Parses a full filename with or without the path and returns the 
 * file extension, determined by everything after the last period.
 * 
 * @param   full filename with or without path
 * @return  extenstion
*/
function getFileExtension($str) 
{
    $i = strrpos($str,".");
    if (!$i) return "";

    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);

    return $ext;
}

//----------------------------------------------------------------------------

function getFilenameSansExt($str) 
{
    $i = strrpos($str,".");
    if (!$i) return "";

    return substr($str,0,$i);
}

//----------------------------------------------------------------------------
/** 
 * Converts a string to a UNIX compatible filename. 
 * Useful for converting an uploaded file's name from an unknown
 * system to a UNIX compatible
 *
 * @param   String to be converted
 * @return  Converted string
*/
function mk_convertFilename($str) 
{

    /*== remove apostrophes ==*/
    $str = str_replace("'","",$str);
    
    /*== remove parentheses ==*/
    $str = str_replace("(","",$str);
    $str = str_replace(")","",$str);

    /*== remove comma ==*/
    $str = str_replace(",","",$str);

    /*== remove all spaces ==*/
    $str = str_replace(" ","",$str);

    return $str;
}

//----------------------------------------------------------------------------

/** 
 * displays a formatted error message
 *
 * @param   errmsg to be displayed
 * @return  Formatted error message
*/
function mk_error($errmsg) 
{
    /*== fix for Netscape table display ==*/
    print "</td></tr></table></table></table></table></div>";
    print "<br><p><div style=\"width:600px; background-color:#CCCCCC; color:red; spacing: 4px\"><font size=4><b>ERROR</b></font></div></p>";
    print "<blockquote>$errmsg</blockquote>";

    exit();
}

function mkerror($errmsg)
{   return mk_error($errmsg); }

//----------------------------------------------------------------------------
//-- HTML/FORM FUNCTIONS
//----------------------------------------------------------------------------

function convert_array_to_options($a)
{
    $retval = "";
    while ($item = each($a)) { $retval .= "<option>$item[1]</option>\n"; }
    return $retval;
}
    
    


//----------------------------------------------------------------------------
//-- TEXT FUNCTIONS
//----------------------------------------------------------------------------

/**
 * displays passed bytes in a more human
 * readable format, kinda like df's -h
 * 
 * @param  byte string to be formated
 * @return formatted string
*/
function format_bytes($bytes)
{
    if ($bytes > 1024)
    { 
        $bytes = $bytes / 1024; 
        $q = "kb";
    }

    if ($bytes > 1024)
    {
        $bytes = $bytes / 1024;
        $q = "mb";
    }

    if (strpos($bytes,"."))
    { $bytes = substr($bytes,0,strpos($bytes,".")+2); }

    return $bytes . $q;
}


/** 
 * Splits a string using the given delimiter into an array.
 *
 * @param   String to be split
 * @param   delimiter
 * @return  array of strings
*/ 
function mksplit($str, $delim) 
{
    return explode($delim, $str);
}

/** 
 * Chops a string to the desired length chopping at a space.
 * errors on the side of shorter length, kinda like word wrap
 * but it cuts. This is useful for preview display purposes 
 * used frequently when linking to the full description.
 *
 * @param   String to be shortened
 * @param   desired maximum length
 * @return  shortened string
*/ 
function mk_substr($str, $len) 
{
    if (strlen($str) < $len)
        return $str;

    $str = substr($str,0,$len);
    if ($spc_pos = strrpos($str," "))
            $str = substr($str,0,$spc_pos);

    return $str . "...";
}
            
function mksubstr($str, $len) 
{ return mk_substr($str,$len); }        

/** 
 * Process text to be inserted into a database.
 * useful when you want to allow different levels
 * of html in the text
 *
 * @param   text to be processed
 * @param   flag to determine processing
 *          flag = 0 ==> strip all tags
 *          flag = 1 ==> allow P,B,I tags - remove php
 *          flag = 2 ==> convert all HTML characters (as is)
 * @return  proccessed text
*/ 
function procDBText($str, $flag) 
{

    if ($flag == 0) { $str = strip_tags($str);  }
    else if ($flag == 1) 
    {
        $str = eregi_replace("<([pbi])>","%%MK\\1MK%%",$str);
        $str = eregi_replace("</([pbi])>","%%MK/\\1MK%%",$str);
        $str = strip_tags($str);
        $str = eregi_replace("%%MK([pbi])MK%%","<\\1>",$str);            
        $str = eregi_replace("%%MK/([pbi])MK%%","</\\1>",$str);            
    }
    else if ($flag == 2) { $str = htmlspecialchars($str); }

    $str = str_replace("'","\'",$str);

    return $str;
}

/** 
 * Process text to be displayed in a browser (HTML)
 * converts carriage returns to <br> tags and converts
 * full URL's (http://) to HTML hyperlinks
 *
 * @param   text to be displayed
 * @return  formatted text
*/ 
function procDisplayText($str) 
{

    // convert carriage returns to <br>
    $str = nl2br($str);

    // convert http://(.*) to <a href="http://(.*)">
    // list valid characters allowed in URL        
    $str = eregi_replace("(http://[-A-Z0-9?=#:&/\.~_]+)","<a href=\"\\1\">\\1</a>",$str);

    return $str;
}

?>
Return current item: Mklib