Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: truncate string to byte count

by haukex (Bishop)
on Feb 28, 2019 at 21:04 UTC ( #1230689=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    sub truncate_utf8 {
       my ($utf8, $len) = @_;
       $len += 0;  # Make sure $len is a number.
       return $utf8 =~ s/^.{0,$len)\K(?![\x80-\xBF]).*//sr;
    }
    
  2. or download this
    use warnings;
    use strict;
    ...
    ok 14
    ok 15
    # Looks like you failed 10 tests of 15.
    
  3. or download this
    sub truncate_utf8 {
       my ($utf8, $len) = @_;
    ...
       $next = chop($utf8) while (ord($next) & 0xC0) == 0x80;
       return $utf8;
    }
    
  4. or download this
    use warnings;
    use strict;
    ...
    ok 14
    ok 15
    # Looks like you failed 10 tests of 15.
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1230689]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2021-03-01 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (28 votes). Check out past polls.

    Notices?