File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/lib/functions.strings.php
<?php
/**
* is_positive_int
*
* Check if a variable is a positive integer
*
* @param string $var
* @param bool $include_zero
*
* @return bool
*/
function is_positive_int($var, $include_zero=true)
{
if (!((int) $var == $var)) {
return false;
}
if ($var < 0) {
return false;
}
if ($var > 0 || $include_zero) {
return true;
}
return false;
}
/**
* This function takes the first argument and checks to see if its value is in any of the other arguments
*
* @return unknown
*/
function enum(){
$args = func_get_args();
$value = $args[0];
$size = sizeof($args);
for($i=1;$i<$size;++$i){
if($value == $args[$i]){
return true;
}
}
return null;
}
/**
* This function checks to see if the needle is at the very end of the haystack
*
* @param string $haystack The haystack
* @param string $needle The needle
* @return boolean True if the end of the haystack is the needle
*/
function str_end($haystack, $needle){
$start = strlen($haystack) - strlen($needle);
if(substr($haystack, $start) == $needle){
return true;
}else{
return false;
}
}
/**
* This function checks to see if the needle is at the very beginning of the haystack
*
* @param string $haystack The haystack
* @param string $needle The needle
* @return boolean True if the beginning of the haystack is the needle
*/
function str_start($haystack, $needle){
return iwp_iStartsWith($haystack, $needle);
}
/**
* is_blank
* This is similar to empty(), but empty() requires a variable.
* e.g. this will *not* work: if( empty(trim($var)) ) as its not checking a variable
* So this function will check is a string is empty, or zero, or null
*
* @return boolean
*/
function is_blank($str, $trim=true){
if($trim) { $str = trim($str); }
if(strlen($str) === 0 || $str == null || iwp_strtolower($str) == "null" || $str === "0" || $str === 0){
return true;
}else{
return false;
}
}
/**
* This function cleans an array and removes any empty elements
*
* @param array $arr The array to be cleaned
* @return array The cleaned array
*/
if(!function_exists('CleanArray')){
function CleanArray($arr){
$new = array();
foreach($arr as $key=>$val){
if($val != ''){
$new[$key] = $val;
}
}
return $new;
}
}
/**
* Returns the last key in an array
*
* @param array $array
* @return string
*/
function array_last_key($array){
end($array);
$key = key($array);
reset($array);
return $key;
}
/**
* stripslashes_deep
* Recursively use stripslashes on an array or a value
*
* @param Mixed $value Array or String to have slashes stripped.
*
* @return Mixed
*/
if(!function_exists('stripslashes_deep')){
function stripslashes_deep($value)
{
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
return $value;
}
}
/**
* StripNewLines
* Removes new lines from a string. Used for things such as email addresses,
* names and other text that should never have a newline.
*
* @param String $str String to have newlines stripped.
*
* @return String
*/
function StripNewLines($str){
$str = str_replace(array("\r\n","\n","\r"),"",$str);
return $str;
}
/**
* elipsize
* Cuts a string to a certain length. If the original string is longer than the
* specified maximum length, a 3 period ellipsis is added on the end.
*
* @param String $str String to elipsize.
* @param Integer $len Maximum string length
*
* @return String
*/
function elipsize($str, $len = 72, $space = true)
{
if (strlen($str) <= $len) {
return $str;
}
if ($space) {
return substr($str, 0, $len - 4) .' ...';
}
return substr($str, 0, $len - 3) .'...';
}
/**
* Reverses nl2br()
*/
if(!function_exists('br2nl')){
function br2nl($text){
return preg_replace('/<br\\\\s*?\\/??>/i', "\\n", $text);
}
}
/**
* in_string
* Works to mimic in_array() but for strings. Uses the strpos() !== false method
* to test if a string is withing another string. The arguements are in the same
* order as in_array() (needle, haystack), which is the reverse of strpos()
*
* @param String $needle The needle to search for
* @param String $haystack The haystack to search within
*
* @return Boolean
*/
if(!function_exists('in_string')){
function in_string($needle,$haystack){
return (bool)(strpos($haystack,$needle)!==false);
}
}
/**
* Rescursive version of in_string for arrays
*
* @param string $in
* @param array $array
*
* @return boolean True if its in the array anywhere, false otherwise
*/
function in_str_array($in,$array){
if(is_array($array)){
foreach($array as $key=>$val){
if(in_str_array($in,$val)){
return true;
}
}
return false;
}else{
return in_string($in,$array);
}
}
/**
* Checks to see if a string is blank, including html empty, i.e. just a new line (<Br>) or a non-breaking space *//*
*
* @param unknown_type $str
* @return unknown
*/
function htmlempty($str){
$str = preg_replace("#<br[ ]*/>#ism", "", $str);
$str = str_replace(" ", "", $str);
$str = str_replace("<br/>", "", $str);
$str = str_replace("\r\n", "", $str);
$str = str_replace("\r", "", $str);
$str = str_replace("\n", "", $str);
if(empty($str)){
return true;
}
$emptyArray[] = '';
$emptyArray[] = ' ';
$emptyArray[] = '<br/>';
$emptyArray[] = ' <br/>';
$emptyArray[] = ' <br/> ';
$emptyArray[] = '<br/> ';
if(in_array(trim($str),$emptyArray)){
return true;
}else{
return false;
}
}
/**
* If the editor is disabled then we'll see if we need to run
* nl2br on the text if it doesn't contain any HTML tags
*/
function FormatWYSIWYGHTML($HTML) {
if(GetConfigBoolean("useWYSIWYGEditor")) {
return $HTML;
}else {
$stripped = strip_tags($HTML);
if($stripped == $HTML) {
// There's no HTML tags, so run nl2br
return nl2br($HTML);
} else {
// It contains HTML tags, leave it as is
return $HTML;
}
}
}
/**
* CheckZero
* This makes sure that a number is always 2 digits or more. So if the input is 3
* it will change it to 03. If its already 2 digits or more, it will leave it as
* it is.
*
* @param mixed $input Can be an integer or a string
* @return mixed If the zero is added, it becomes a string, otherwise it is what
* ever $input was.
*/
function CheckZero($input){
if(strlen($input) < 2){
return (string)'0'.$input;
} else {
return $input;
}
}
/**
* ResizeHTMLImages
* This function takes a chunck of HTML and searches for <img> HTML tags. It will
* then alter the width and height values to a maximum size while maintaining
* aspect ratio .
*
* @param String $content HTML Content
* @param Integer $maxwidth Maximum width value
* @param Integer $maxheight Maximum height value
*
* @return String
*/
function ResizeHTMLImages($content,$maxwidth=100,$maxheight=100){
preg_match_all("#(\<img[^>]*>)#ims",$content,$results);
foreach ($results[1] as $key=>$value){
$original = $value;
$new = $value;
// check and replace width
preg_match_all("#width=['\"]([0-9]*)['\"]#ims",$original,$width);
if($width[1][0] > $maxwidth){
$new = str_replace($width[0][0], 'width="'.$maxwidth.'"',$new);
}
// check and replace height
preg_match_all("#height=['\"]([0-9]*)['\"]#ims",$original,$height);
if($height[1][0] > $maxheight){
$new = str_replace($height[0][0], 'height="'.$maxheight.'"',$new);
}
$content = str_replace($original,$new,$content);
}
return $content;
}
/**
* Same as ResizeHTMLImages but uses the width to scale the ratio
*
* @param string $content
* @param integer $maxwidth
*
* @return string HTML
*/
function ResizeHTMLImagesByWidth($content,$maxwidth=100){
preg_match_all("#(\<img[^>]*>)#ims",$content,$results);
foreach ($results[1] as $key=>$value){
$original = $value;
$new = $value;
// check and replace width
preg_match_all("#width=['\"]([0-9]*)['\"]#ims",$original,$width);
if($width[1][0] > $maxwidth){
$new = str_replace($width[0][0], 'width="'.$maxwidth.'"',$new);
// check and replace height
preg_match_all("#height=['\"]([0-9]*)['\"]#ims",$original,$height);
$newheight = $height[1][0] * ($maxwidth / $width[1][0]);
$new = str_replace($height[0][0], 'height="'.$maxheight.'"',$new);
}
$content = str_replace($original,$new,$content);
}
return $content;
}
/**
* CheckImageFilename
* Function to detect if the filename parsed is a valid basic image file.
*
* @return Boolean
*/
function CheckImageFilename($Filename){
$Filename = iwp_strtolower($Filename);
$ValidFiles3 = array(".png",".jpg",".gif",".bmp"); // 3 letter extensions
$ValidFiles4 = array(".jpeg",".tiff"); // 4 letter extensions
if(!in_array(substr($Filename,-4),$ValidFiles3) && !in_array(substr($Filename,-5),$ValidFiles4)){
// not in the arrays
return false;
}else{
// is in the arrays
return true;
}
}
/**
* makeUrlSafe
* Special function to urlencode a string while replacing certain values, such as
* spaces to other values to make the URLs have a better visual appeal.
*
* @param String $val String to encode
*
* @return String
*/
function makeUrlSafe($val)
{
$targets = array(' ', '/');
$replaces = array('-', '{47}');
return urlencode(str_replace($targets, $replaces, $val));
}
/**
* NiceSize
*
* Returns a datasize formatted into the most relevant units
* @return string The formatted filesize
* @param int Size In Bytes
*/
function NiceSize($SizeInBytes=0)
{
if ($SizeInBytes > 1024 * 1024 * 1024) {
$suffix = 'GB';
return sprintf("%01.2f %s", $SizeInBytes / (1024 * 1024 * 1024), $suffix);
} elseif ($SizeInBytes > 1024 * 1024 ) {
$suffix = 'MB';
return sprintf("%01.2f %s", $SizeInBytes / (1024 * 1024), $suffix);
} elseif ($SizeInBytes > 1024) {
$suffix = 'KB';
return sprintf("%01.2f %s", $SizeInBytes / 1024, $suffix);
} elseif ($SizeInBytes < 1024) {
$suffix = 'B';
return sprintf("%d %s", $SizeInBytes, $suffix);
}
}
/**
* StripFontTags
*
* Removes the font family names, so all the fonts are consistant
* @param string The string to remove the font name from
* @return string The reformatted string
*/
function StripFontFamily($str){
$patterns[0] = '/\<font([^>])*face\="([^"]*)"/i';
$patterns[1] = '/\<([\w]+) style="([^"]*)font-family\:[^;"]*[;]?([^"]*)"/i';
$patterns[2] = '/\<([\w]+) style=" "/i';
$replacements[0] = '<font $1';
$replacements[1] = '<$1 style="$2 $3"';
$replacements[2] = '<$1';
$str = preg_replace($patterns, $replacements, $str);
return $str;
}
/**
* HeadersToArray
*
* Turns HTTP headers into an associative array
* @param string The string with the header, such as delivered by the getFileThroughURLFopen function
* @return array Array with the header information
*/
function HeadersToArray($h)
{
$ret = array();
$lines = explode("\n", $h);
foreach($lines as $line)
{
$line = trim($line);
$char_cut = strpos($line, ":");
if ($char_cut===false)
{
continue;
}
$ret[substr($line, 0, $char_cut)] = trim(substr($line, $char_cut+1));
}
return $ret;
}
/**
* limitSizeTo
*
* Cuts a string to the length $ln adding a '...' in the middle of the string $str.
*
* @param string $st The string to be cut
* @param int $ln The desirable length
* @return string Cut string (doh!). The total length of this string INCLUDES the '...'... roughly
*/
function limitSizeTo($str, $ln)
{
if (strlen($str)<=$ln)
{
return $str;
}
$remove = strlen($str) - $ln;
$middle = floor($ln/2)-1;
$first_bit = substr($str,0,$middle);
$last_bit = substr($str,strlen($str)-$middle);
return $first_bit . '...' . $last_bit;
}
function iwp_iStartsWith($str, $start){
if(substr($str, 0, strlen($start)) == $start){
return true;
}else{
return false;
}
}
/**
* Returns the right-most $n characters of string $str
*
* @param string $str
* @param int $n
* @return string
*/
function right ($str, $n) {
return substr($str, strlen($str) - $n, $n);
}
define('IWP_MULTIBYTE_STRINGS', function_exists('mb_internal_encoding') && function_exists('mb_strtolower'));
if (IWP_MULTIBYTE_STRINGS) {
mb_internal_encoding('UTF-8');
function iwp_strtolower ($str) {
return mb_strtolower($str);
}
function iwp_strtoupper ($str) {
return mb_strtoupper($str);
}
} else {
function iwp_strtolower ($str) {
return strtolower($str);
}
function iwp_strtoupper ($str) {
return strtoupper($str);
}
}