<?php
/**
 * Healthcoach Framework: strings manipulations
 *
 * @package	healthcoach
 * @since	healthcoach 1.0
 */

// Disable direct call
if ( ! defined( 'ABSPATH' ) ) { exit; }

// Check multibyte functions
if ( ! defined( 'HEALTHCOACH_MULTIBYTE' ) ) define( 'HEALTHCOACH_MULTIBYTE', function_exists('mb_strpos') ? 'UTF-8' : false );

if (!function_exists('healthcoach_strlen')) {
	function healthcoach_strlen($text) {
		return HEALTHCOACH_MULTIBYTE ? mb_strlen($text) : strlen($text);
	}
}

if (!function_exists('healthcoach_strpos')) {
	function healthcoach_strpos($text, $char, $from=0) {
		return HEALTHCOACH_MULTIBYTE ? mb_strpos($text, $char, $from) : strpos($text, $char, $from);
	}
}

if (!function_exists('healthcoach_strrpos')) {
	function healthcoach_strrpos($text, $char, $from=0) {
		return HEALTHCOACH_MULTIBYTE ? mb_strrpos($text, $char, $from) : strrpos($text, $char, $from);
	}
}

if (!function_exists('healthcoach_substr')) {
	function healthcoach_substr($text, $from, $len=-999999) {
		if ($len==-999999) { 
			if ($from < 0)
				$len = -$from; 
			else
				$len = healthcoach_strlen($text)-$from;
		}
		return HEALTHCOACH_MULTIBYTE ? mb_substr($text, $from, $len) : substr($text, $from, $len);
	}
}

if (!function_exists('healthcoach_strtolower')) {
	function healthcoach_strtolower($text) {
		return HEALTHCOACH_MULTIBYTE ? mb_strtolower($text) : strtolower($text);
	}
}

if (!function_exists('healthcoach_strtoupper')) {
	function healthcoach_strtoupper($text) {
		return HEALTHCOACH_MULTIBYTE ? mb_strtoupper($text) : strtoupper($text);
	}
}

if (!function_exists('healthcoach_strtoproper')) {
	function healthcoach_strtoproper($text) { 
		$rez = ''; $last = ' ';
		for ($i=0; $i<healthcoach_strlen($text); $i++) {
			$ch = healthcoach_substr($text, $i, 1);
			$rez .= healthcoach_strpos(' .,:;?!()[]{}+=', $last)!==false ? healthcoach_strtoupper($ch) : healthcoach_strtolower($ch);
			$last = $ch;
		}
		return $rez;
	}
}

if (!function_exists('healthcoach_strrepeat')) {
	function healthcoach_strrepeat($str, $n) {
		$rez = '';
		for ($i=0; $i<$n; $i++)
			$rez .= $str;
		return $rez;
	}
}

if (!function_exists('healthcoach_strshort')) {
	function healthcoach_strshort($str, $maxlength, $add='...') {
		if ($maxlength < 0) 
			return $str;
		if ($maxlength == 0) 
			return '';
		if ($maxlength >= healthcoach_strlen($str)) 
			return strip_tags($str);
		$str = healthcoach_substr(strip_tags($str), 0, $maxlength - healthcoach_strlen($add));
		$ch = healthcoach_substr($str, $maxlength - healthcoach_strlen($add), 1);
		if ($ch != ' ') {
			for ($i = healthcoach_strlen($str) - 1; $i > 0; $i--)
				if (healthcoach_substr($str, $i, 1) == ' ') break;
			$str = trim(healthcoach_substr($str, 0, $i));
		}
		if (!empty($str) && healthcoach_strpos(',.:;-', healthcoach_substr($str, -1))!==false) $str = healthcoach_substr($str, 0, -1);
		return ($str) . ($add);
	}
}

// Clear string from spaces, line breaks and tags (only around text)
if (!function_exists('healthcoach_strclear')) {
	function healthcoach_strclear($text, $tags=array()) {
		if (empty($text)) return $text;
		if (!is_array($tags)) {
			if ($tags != '')
				$tags = explode($tags, ',');
			else
				$tags = array();
		}
		$text = trim(chop($text));
		if (is_array($tags) && count($tags) > 0) {
			foreach ($tags as $tag) {
				$open  = '<'.esc_attr($tag);
				$close = '</'.esc_attr($tag).'>';
				if (healthcoach_substr($text, 0, healthcoach_strlen($open))==$open) {
					$pos = healthcoach_strpos($text, '>');
					if ($pos!==false) $text = healthcoach_substr($text, $pos+1);
				}
				if (healthcoach_substr($text, -healthcoach_strlen($close))==$close) $text = healthcoach_substr($text, 0, healthcoach_strlen($text) - healthcoach_strlen($close));
				$text = trim(chop($text));
			}
		}
		return $text;
	}
}

// Return slug for the any title string
if (!function_exists('healthcoach_get_slug')) {
	function healthcoach_get_slug($title) {
		return healthcoach_strtolower(str_replace(array('\\','/','-',' ','.'), '_', $title));
	}
}

// Replace macros in the string
if (!function_exists('healthcoach_strmacros')) {
	function healthcoach_strmacros($str) {
		return str_replace(array("{{", "}}", "((", "))", "||"), array("<i>", "</i>", "<b>", "</b>", "<br>"), $str);
	}
}

// Unserialize string (try replace \n with \r\n)
if (!function_exists('healthcoach_unserialize')) {
	function healthcoach_unserialize($str) {
		if ( is_serialized($str) ) {
			try {
				$data = unserialize($str);
			} catch (Exception $e) {
				dcl($e->getMessage());
				$data = false;
			}
			if ($data===false) {
				try {
					$data = unserialize(str_replace("\n", "\r\n", $str));
				} catch (Exception $e) {
					dcl($e->getMessage());
					$data = false;
				}
			}
			return $data;
		} else
			return $str;
	}
}
?>