<?php
/**
 * SeoHub Framework: strings manipulations
 *
 * @package	seohub
 * @since	seohub 1.0
 */

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

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

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

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

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

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

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

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

if (!function_exists('seohub_strtoproper')) {
	function seohub_strtoproper($text) { 
		$rez = ''; $last = ' ';
		for ($i=0; $i<seohub_strlen($text); $i++) {
			$ch = seohub_substr($text, $i, 1);
			$rez .= seohub_strpos(' .,:;?!()[]{}+=', $last)!==false ? seohub_strtoupper($ch) : seohub_strtolower($ch);
			$last = $ch;
		}
		return $rez;
	}
}

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

if (!function_exists('seohub_strshort')) {
	function seohub_strshort($str, $maxlength, $add='...') {
	//	if ($add && seohub_substr($add, 0, 1) != ' ')
	//		$add .= ' ';
		if ($maxlength < 0) 
			return $str;
		if ($maxlength < 1 || $maxlength >= seohub_strlen($str)) 
			return strip_tags($str);
		$str = seohub_substr(strip_tags($str), 0, $maxlength - seohub_strlen($add));
		$ch = seohub_substr($str, $maxlength - seohub_strlen($add), 1);
		if ($ch != ' ') {
			for ($i = seohub_strlen($str) - 1; $i > 0; $i--)
				if (seohub_substr($str, $i, 1) == ' ') break;
			$str = trim(seohub_substr($str, 0, $i));
		}
		if (!empty($str) && seohub_strpos(',.:;-', seohub_substr($str, -1))!==false) $str = seohub_substr($str, 0, -1);
		return ($str) . ($add);
	}
}

// Clear string from spaces, line breaks and tags (only around text)
if (!function_exists('seohub_strclear')) {
	function seohub_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 (seohub_substr($text, 0, seohub_strlen($open))==$open) {
					$pos = seohub_strpos($text, '>');
					if ($pos!==false) $text = seohub_substr($text, $pos+1);
				}
				if (seohub_substr($text, -seohub_strlen($close))==$close) $text = seohub_substr($text, 0, seohub_strlen($text) - seohub_strlen($close));
				$text = trim(chop($text));
			}
		}
		return $text;
	}
}

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

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

// Unserialize string (try replace \n with \r\n)
if (!function_exists('seohub_unserialize')) {
	function seohub_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;
				}
			}
			//if ($data===false) $data = @unserialize(str_replace(array("\n", "\r"), array('\\n','\\r'), $str));
			return $data;
		} else
			return $str;
	}
}
?>