Wie würden Sie eine Zeichenfolge mitteilen, die binäre Daten aus text enthält? Ich versuche zu implementieren: < /p>
[*] Vollständige PHP < /strong> Lösung < /li>
mit NEIN externe Bibliotheken
[*] codieren-bewusst
[*] Konsistent
und Fast < /stark> genug, um lange Streams zu bewältigen < /li>
< /ul>
Hier ist die beste Lösung, die ich begegnet bin (unter allen nicht arbeitenden), die auf Mbstring beruhen Funktionsfamilie:
function isBinaryStream(string $stream) : bool {
// 1) Try to detect encoding
// $encoding is a prioritized list of encodings (from less widely used to most widely used) for many Western and East Asian applications
$encoding = mb_detect_encoding($stream, [
'UTF-8', 'GB18030', 'BIG-5', 'EUC-JP', 'SJIS', 'ASCII', 'Windows-1252', 'ISO-8859-1', 'Windows-1251', 'KOI8-R',
], true);
if ($encoding !== 'UTF-8') {
$stream = mb_convert_encoding($stream, 'UTF-8', $encoding);
}
// 2) Split into characters and convert to code points
$chars = mb_str_split($stream, 1, 'UTF-8');
if (! $chars) {
return true;
}
$ordValues = array_map('mb_ord', $chars);
// 3) Disallow control chars except \t(9), \n(10), \r(13) and disallow code points above 0x10FFFF (invalid Unicode)
foreach ($ordValues as $ord) {
if ($ord < 0x20 && ! in_array($ord, [9, 10, 13], true)) {
return true;
}
if ($ord > 0x10FFFF) {
return true;
}
}
// If we are here, we consider $stream textual
return false;
}
< /code>
Ich habe versucht, KI zu fragen, aber seine Implementierung gab mir viele falsch positive positive Ergebnisse für eine riesige Reihe von Textdateien, die spärliche fehlerhafte Zeichen enthielten. < /p>
// ATTENTION: this code is AI-generated. Don't blindly copy-paste it.
function syntheticIsBinaryStream(string $stream) : bool {
// ATTENTION: this code is AI-generated. Don't blindly copy-paste it.
// Define a prioritized list of encodings.
$encodings = ['UTF-8', 'ASCII', 'Windows-1252', 'ISO-8859-1'];
$validEncoding = false;
foreach ($encodings as $enc) {
// Strict check: only return true if the sample is valid in the encoding.
if (mb_check_encoding($stream, $enc)) {
$validEncoding = $enc;
break;
}
}
if ($validEncoding === false) {
// If none of our encodings validate the sample, assume binary.
return true;
}
// If encoding is not UTF-8, convert to UTF-8 for consistent further processing.
if ($validEncoding !== 'UTF-8') {
$stream = mb_convert_encoding($stream, 'UTF-8', $validEncoding);
}
// Now, count disallowed control characters (any Cc that is not tab, newline, or carriage return)
// Using Unicode properties, this regex matches any control char except \r, \n, \t.
if (preg_match_all('/[\p{Cc}&&[^\r\n\t]]/u', $stream, $matches)) {
$controlCount = count($matches[0]);
} else {
$controlCount = 0;
}
// Compute total number of characters in the stream.
$totalChars = mb_strlen($stream, 'UTF-8');
if ($totalChars === 0) {
return true;
}
$ratio = $controlCount / $totalChars;
// If the ratio of disallowed control characters is above 10%, we consider the sample as binary.
return $ratio > 0.10;
}
< /code>
ausprobiert diese Lösung auch mit CTYPE_PRINT, und diese gab mir auch viele falsche positive Ergebnisse. IsBinaryStream
Mangel an Konsistenz. Ich bin mir nicht sicher, ob dies für nur Text-Streams für nicht-westliche Kodierungen funktionieren würde. Zusätzlich schwöre ich, dass es enorm optimiert werden kann. < /P>
Was schlagen Sie vor? Vielen Dank für Ihren Rat.
Wie würden Sie eine Zeichenfolge mitteilen, die binäre Daten aus text enthält? Ich versuche zu implementieren: < /p>
[*] [b] Vollständige PHP < /strong> Lösung < /li> mit NEIN [/b] externe Bibliotheken [*] codieren-bewusst [*] Konsistent und Fast < /stark> genug, um lange Streams zu bewältigen < /li> < /ul> Hier ist die beste Lösung, die ich begegnet bin (unter allen nicht arbeitenden), die auf Mbstring beruhen Funktionsfamilie: [code]function isBinaryStream(string $stream) : bool { // 1) Try to detect encoding // $encoding is a prioritized list of encodings (from less widely used to most widely used) for many Western and East Asian applications $encoding = mb_detect_encoding($stream, [ 'UTF-8', 'GB18030', 'BIG-5', 'EUC-JP', 'SJIS', 'ASCII', 'Windows-1252', 'ISO-8859-1', 'Windows-1251', 'KOI8-R', ], true); if ($encoding !== 'UTF-8') { $stream = mb_convert_encoding($stream, 'UTF-8', $encoding); }
// 2) Split into characters and convert to code points $chars = mb_str_split($stream, 1, 'UTF-8'); if (! $chars) { return true; } $ordValues = array_map('mb_ord', $chars);
// 3) Disallow control chars except \t(9), \n(10), \r(13) and disallow code points above 0x10FFFF (invalid Unicode) foreach ($ordValues as $ord) { if ($ord < 0x20 && ! in_array($ord, [9, 10, 13], true)) { return true; } if ($ord > 0x10FFFF) { return true; } }
// If we are here, we consider $stream textual return false; } < /code> Ich habe versucht, KI zu fragen, aber seine Implementierung gab mir viele falsch positive positive Ergebnisse für eine riesige Reihe von Textdateien, die spärliche fehlerhafte Zeichen enthielten. < /p> // ATTENTION: this code is AI-generated. Don't blindly copy-paste it. function syntheticIsBinaryStream(string $stream) : bool { // ATTENTION: this code is AI-generated. Don't blindly copy-paste it.
// Define a prioritized list of encodings. $encodings = ['UTF-8', 'ASCII', 'Windows-1252', 'ISO-8859-1']; $validEncoding = false; foreach ($encodings as $enc) { // Strict check: only return true if the sample is valid in the encoding. if (mb_check_encoding($stream, $enc)) { $validEncoding = $enc; break; } }
if ($validEncoding === false) { // If none of our encodings validate the sample, assume binary. return true; }
// If encoding is not UTF-8, convert to UTF-8 for consistent further processing. if ($validEncoding !== 'UTF-8') { $stream = mb_convert_encoding($stream, 'UTF-8', $validEncoding); }
// Now, count disallowed control characters (any Cc that is not tab, newline, or carriage return) // Using Unicode properties, this regex matches any control char except \r, \n, \t. if (preg_match_all('/[\p{Cc}&&[^\r\n\t]]/u', $stream, $matches)) { $controlCount = count($matches[0]); } else { $controlCount = 0; }
// Compute total number of characters in the stream. $totalChars = mb_strlen($stream, 'UTF-8'); if ($totalChars === 0) { return true; } $ratio = $controlCount / $totalChars;
// If the ratio of disallowed control characters is above 10%, we consider the sample as binary. return $ratio > 0.10; } < /code> ausprobiert diese Lösung auch mit CTYPE_PRINT, und diese gab mir auch viele falsche positive Ergebnisse. IsBinaryStream [/code] Mangel an Konsistenz. Ich bin mir nicht sicher, ob dies für nur Text-Streams für nicht-westliche Kodierungen funktionieren würde. Zusätzlich schwöre ich, dass es enorm optimiert werden kann. < /P> Was schlagen Sie vor? Vielen Dank für Ihren Rat.
Eigentliches Projekt
Ich möchte ein wiederverwendbares Steuerelement Numpad erstellen, das über eine bindbare Eigenschaft Text verfügt. Durch Drücken einer beliebigen Taste im Numpad wird eine...
Ich möchte eine dieser Serialisierungsbibliotheken verwenden, um einen STL -Behälter zu archivieren, der Rohzeiger auf ein Objekt hält. Ich möchte jedoch weder die Zeiger noch die Objekte, auf die...
Ich arbeite mit C#und dem neuesten Windows UI -Automatisierungsframework. Ich versuche, auf einer bestimmten Seite automatisch die Registerkarte durch alle Steuerelemente zu durchsuchen, und möchte...