Skip to content

Commit

Permalink
Use iconv() instead of mb_convert_encoding() when the charset is not …
Browse files Browse the repository at this point in the history
…in mb_list_encodings()
  • Loading branch information
duzun committed Jul 17, 2018
1 parent 43c7ab2 commit f2f8ffc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "duzun/hquery",
"type": "library",
"description": "An extremely fast web scraper that parses megabytes of HTML in a blink of an eye. No dependencies. PHP5+",
"version": "2.0.3",
"version": "2.1.0",
"license": "MIT",
"authors": [
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hquery.php",
"description": "An extremely fast web scraper that parses megabytes of HTML in a blink of an eye. No dependencies. PHP5+",
"version": "2.0.3",
"version": "2.1.0",
"author": {
"name": "Dumitru Uzun",
"email": "contact@duzun.me",
Expand Down
2 changes: 1 addition & 1 deletion src/hQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class_exists('duzun\\hQuery\\HTML_Parser', false) or require_once __DIR__ . DIRE
*
* @author Dumitru Uzun (DUzun.ME)
* @license MIT
* @version 2.0.3
* @version 2.1.0
*/
class hQuery extends hQuery\HTML_Parser {

Expand Down
38 changes: 30 additions & 8 deletions src/hQuery/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
*/
abstract class Node implements \Iterator, \Countable {
// ------------------------------------------------------------------------
const VERSION = '2.0.3';
const VERSION = '2.1.0';
// ------------------------------------------------------------------------
public static $last_http_result; // Response details of last request

// ------------------------------------------------------------------------
public static $selected_doc = NULL;

// ------------------------------------------------------------------------
protected $_prop = array(); // Properties
protected $doc; // Parent doc
protected $ids; // contained elements' IDs
protected $exc; // excluded elements' IDs

protected static $_mb_encodings;
// ------------------------------------------------------------------------
public $tag_map; // map tag names (eg ['b' => 'strong', 'i' => 'em'])

Expand Down Expand Up @@ -923,31 +925,51 @@ static function array_select($arr, $keys, $force_null=false) {
}
return $ret;
}

// ------------------------------------------------------------------------
static function convert_encoding($a, $to, $from=NULL) {
static function is_mb_charset_supported($charset) {
if ( !isset(self::$_mb_encodings) ) {
if ( !function_exists('mb_list_encodings') ) return false;
self::$_mb_encodings = array_change_key_case(
array_flip(mb_list_encodings()),
CASE_UPPER
);
}
return isset(self::$_mb_encodings[strtoupper($charset)]);
}

// ------------------------------------------------------------------------
static function convert_encoding($a, $to, $from=NULL, $use_mb=NULL) {
static $meth = NULL;
isset($meth) or $meth = function_exists('mb_convert_encoding');
isset($from) or $from = $meth ? mb_internal_encoding() : iconv_get_encoding('internal_encoding');

if ( !isset($use_mb) ) {
$use_mb = $meth && self::is_mb_charset_supported($to) && (!isset($from) || self::is_mb_charset_supported($from));
}
elseif( $use_mb && !$meth ) {
$use_mb = false;
}
isset($from) or $from = $use_mb ? mb_internal_encoding() : iconv_get_encoding('internal_encoding');

if(is_array($a)) {
$ret = array();
foreach($a as $n => $v) {
$ret[is_string($n)?self::convert_encoding($n,$to,$from):$n] = is_string($v) || is_array($v) || $v instanceof stdClass
? self::convert_encoding($v, $to, $from)
$ret[is_string($n)?self::convert_encoding($n,$to,$from,$use_mb):$n] = is_string($v) || is_array($v) || $v instanceof stdClass
? self::convert_encoding($v, $to, $from, $use_mb)
: $v;
}
return $ret;
}
elseif($a instanceof stdClass) {
$ret = (object)array();
foreach($a as $n => $v) {
$ret->{is_string($n)?self::convert_encoding($n,$to,$from):$n} = is_string($v) || is_array($v) || $v instanceof stdClass
? self::convert_encoding($v, $to, $from)
$ret->{is_string($n)?self::convert_encoding($n,$to,$from, $use_mb):$n} = is_string($v) || is_array($v) || $v instanceof stdClass
? self::convert_encoding($v, $to, $from, $use_mb)
: $v;
}
return $ret;
}
return is_string($a) ? $meth ? mb_convert_encoding($a, $to, $from) : iconv($from, $to, $a) : $a;
return is_string($a) ? $use_mb ? mb_convert_encoding($a, $to, $from) : iconv($from, $to, $a) : $a;
}
// ------------------------------------------------------------------------
}
Expand Down

0 comments on commit f2f8ffc

Please sign in to comment.