/** * Converts the string into an valid UTF-8 string. * * @return Stringy */ public function utf8ify() { return static::create(UTF8::cleanup($this->str)); }
/** * escape * * @param array|float|int|string|boolean $var boolean: convert into "integer"<br /> * int: convert into "integer"<br /> * float: convert into "float" and replace "," with "."<br /> * array: run escape() for every key => value<br /> * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> * @param bool $stripe_non_utf8 * @param bool $html_entity_decode * @param bool $array_to_string * * @return array|bool|float|int|string */ public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = true, $array_to_string = false) { if (is_int($var) || is_bool($var)) { // int return (int) $var; } elseif (is_float($var)) { // float return number_format((double) str_replace(',', '.', $var), 8, '.', ''); } elseif (is_array($var)) { // array $varCleaned = array(); foreach ($var as $key => $value) { $key = (string) $this->escape($key, $stripe_non_utf8, $html_entity_decode); $value = (string) $this->escape($value, $stripe_non_utf8, $html_entity_decode); $varCleaned[$key] = $value; } if ($array_to_string === true) { $varCleaned = implode(',', $varCleaned); return $varCleaned; } else { return (array) $varCleaned; } } if (is_string($var)) { // string if ($stripe_non_utf8 === true) { $var = UTF8::cleanup($var); } if ($html_entity_decode === true) { // use no-html-entity for db $var = UTF8::html_entity_decode($var); } $var = get_magic_quotes_gpc() ? stripslashes($var) : $var; $var = mysqli_real_escape_string($this->getLink(), $var); return (string) $var; } else { return false; } }
/** * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. * * @param mixed $var boolean: convert into "integer"<br /> * int: int (don't change it)<br /> * float: float (don't change it)<br /> * null: null (don't change it)<br /> * array: run escape() for every key => value<br /> * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> * @param bool $stripe_non_utf8 * @param bool $html_entity_decode * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> * <strong>true</strong> => Convert to string var1,var2,var3...<br /> * <strong>null</strong> => Convert the array into null, every time. * * @return mixed */ public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) { if ($var === null) { return null; } // save the current value as int (for later usage) if (!is_object($var)) { $varInt = (int) $var; } /** @noinspection TypeUnsafeComparisonInspection */ if (is_int($var) || is_bool($var) || isset($varInt, $var[0]) && $var[0] != '0' && "{$varInt}" == $var) { // "int" || int || bool return (int) $var; } elseif (is_float($var)) { // float return $var; } elseif (is_array($var)) { // array if ($convert_array === null) { return null; } $varCleaned = array(); foreach ((array) $var as $key => $value) { $key = $this->escape($key, $stripe_non_utf8, $html_entity_decode); $value = $this->escape($value, $stripe_non_utf8, $html_entity_decode); /** @noinspection OffsetOperationsInspection */ $varCleaned[$key] = $value; } if ($convert_array === true) { $varCleaned = implode(',', $varCleaned); return $varCleaned; } else { return (array) $varCleaned; } } if (is_string($var) || is_object($var) && method_exists($var, '__toString')) { // "string" $var = (string) $var; if ($stripe_non_utf8 === true) { $var = UTF8::cleanup($var); } if ($html_entity_decode === true) { // use no-html-entity for db $var = UTF8::html_entity_decode($var); } $var = get_magic_quotes_gpc() ? stripslashes($var) : $var; $var = \mysqli_real_escape_string($this->getLink(), $var); return (string) $var; } elseif ($var instanceof \DateTime) { // "DateTime"-object try { return $this->escape($var->format('Y-m-d H:i:s'), false); } catch (\Exception $e) { return null; } } else { return false; } }