is there any way we can check if string needs decoding in utf-8.
Put more clearly, you are asking if there's a way to tell whether a string has already been decoded, or whether it's still encoded using UTF-8.
There's no way to tell for sure.
You could use something like the following:
sub is_valid_utf8 {
return $_[0] =~ /
^
(?: [\x00-\x7F]
| [\xC0-\xDF] [\x80-\xBF]
| [\xE0-\xEF] [\x80-\xBF]{2}
| [\xF0-\xF7] [\x80-\xBF]{3}
)*+
\z
/x
}
utf8::decode($string)
if is_valid_utf8($string);
This simplifies to
utf8::decode($string);
This won't always work. Certain decoded strings are valid UTF-8. That said, these strings would likely be nonsense. I think the conditions I listed here would apply. So the above is actually quite reliable.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|