Skip to content

Commit 85a53c2

Browse files
committed
Upd. CleanTalk helper. Use special method UTF8 decoding.
1 parent 0bada7e commit 85a53c2

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

lib/CleantalkHelper.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,32 @@ public static function stringToUTF8($str, $data_codepage = null)
343343
}
344344

345345
/**
346-
* Function convert string from UTF8
347-
* param string
348-
* param string
349-
* @return string
350-
*/
351-
public static function stringFromUTF8($str, $data_codepage = null)
352-
{
353-
if(preg_match('u', $str) && function_exists('mb_convert_encoding') && $data_codepage !== null)
354-
return mb_convert_encoding($str, $data_codepage, 'UTF-8');
355-
356-
return $str;
357-
}
346+
* Function convert from UTF8
347+
*
348+
* @param array|object|string $obj
349+
* @param string $data_codepage
350+
*
351+
* @return mixed (array|object|string)
352+
*/
353+
public static function stringFromUTF8($obj, $data_codepage = null)
354+
{
355+
// Array || object
356+
if (is_array($obj) || is_object($obj)) {
357+
foreach ($obj as $_key => &$val) {
358+
$val = self::stringFromUTF8($val, $data_codepage);
359+
}
360+
unset($val);
361+
//String
362+
} else {
363+
if ($data_codepage !== null && preg_match('//u', $obj)) {
364+
if ( function_exists('mb_convert_encoding') ) {
365+
$obj = mb_convert_encoding($obj, $data_codepage, 'UTF-8');
366+
} elseif (version_compare(phpversion(), '8.3', '<')) {
367+
$obj = @utf8_decode($obj);
368+
}
369+
}
370+
}
371+
372+
return $obj;
373+
}
358374
}

lib/CleantalkResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ function __construct($response = null, $obj = null) {
150150
? preg_replace("/.+(\*\*\*.+\*\*\*).+/", "$1", $this->errstr)
151151
: '';
152152

153-
$this->stop_words = isset($obj->stop_words) ? utf8_decode($obj->stop_words) : null;
154-
$this->comment = isset($obj->comment) ? utf8_decode($obj->comment) : null;
153+
$this->stop_words = isset($obj->stop_words) ? CleantalkHelper::stringFromUTF8($obj->stop_words, 'ISO-8859-1') : null;
154+
$this->comment = isset($obj->comment) ? CleantalkHelper::stringFromUTF8($obj->comment, 'ISO-8859-1') : null;
155155
$this->blacklisted = (isset($obj->blacklisted)) ? $obj->blacklisted : null;
156156
$this->allow = (isset($obj->allow)) ? $obj->allow : 0;
157157
$this->id = (isset($obj->id)) ? $obj->id : null;

0 commit comments

Comments
 (0)