/application/library/Tg/String.php

https://github.com/sandboxdigital/zephyr-core · PHP · 55 lines · 40 code · 8 blank · 7 comment · 6 complexity · 4383d4e6e47048577b2b94465db01995 MD5 · raw file

  1. <?php
  2. class Tg_String {
  3. private $_string = '';
  4. function __construct($str)
  5. {
  6. $this->_string = $str;
  7. }
  8. function startsWith($needle,$case=true) {
  9. if($case){return (strcmp(substr($this->_string, 0, strlen($needle)),$needle)===0);}
  10. return (strcasecmp(substr($this->_string, 0, strlen($needle)),$needle)===0);
  11. }
  12. function endsWith($needle,$case=true) {
  13. if($case){return (strcmp(substr($this->_string, strlen($this->_string) - strlen($needle)),$needle)===0);}
  14. return (strcasecmp(substr($this->_string, strlen($this->_string) - strlen($needle)),$needle)===0);
  15. }
  16. public static function sanitizeUrl($z)
  17. {
  18. $z = strtolower($z);
  19. $z = preg_replace('/[^a-z0-9 -]+/', '', $z);
  20. $z = str_replace(' ', '-', $z);
  21. return trim($z, '-');
  22. }
  23. /**
  24. * @static
  25. * @param $hexStr
  26. * @param bool $returnAsString
  27. * @param string $seperator
  28. * @return array|string
  29. */
  30. public static function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {
  31. $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
  32. $rgbArray = array();
  33. if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster
  34. $colorVal = hexdec($hexStr);
  35. $rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
  36. $rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
  37. $rgbArray['blue'] = 0xFF & $colorVal;
  38. } elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
  39. $rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
  40. $rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
  41. $rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
  42. } else {
  43. return false; //Invalid hex color code
  44. }
  45. return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array
  46. }
  47. }