CharsetHelper::detect¶
(PHP 7 >= 7.4.0, PHP 8)
CharsetHelper::detect — Detect charset encoding of a string
Description¶
public static CharsetHelper::detect(string $string, array $options = []): string
Automatically detects the character encoding of a string using multiple detection strategies with fallback (mb_detect_encoding → FileInfo).
Parameters¶
string:
The string to analyze.
options:
Optional array of detection options:
encodings(array): List of encodings to test (default: ['UTF-8', 'CP1252', 'ISO-8859-1', 'ASCII'])
Return Values¶
Returns the detected encoding as an uppercase string (e.g., 'UTF-8', 'ISO-8859-1').
Examples¶
Example #1 Basic encoding detection¶
<?php
use Ducks\Component\Component\EncodingRepair\CharsetHelper;
$string = file_get_contents('unknown.txt');
$encoding = CharsetHelper::detect($string);
echo "Detected: {$encoding}";
Example #2 Custom encoding list¶
<?php
use Ducks\Component\Component\EncodingRepair\CharsetHelper;
$encoding = CharsetHelper::detect($string, [
'encodings' => ['UTF-8', 'Shift_JIS', 'EUC-JP']
]);
See Also¶
- CharsetHelper::toCharset — Convert with auto-detection
- mb_detect_encoding() — PHP native detection function