Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: truncate string to byte count

by ikegami (Patriarch)
on Feb 28, 2019 at 20:43 UTC ( [id://1230687]=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
    sub truncate_utf8 {
       my ($utf8, $len) = @_;
    ...
       $next = chop($utf8) while (ord($next) & 0xC0) == 0x80;
       return $utf8;
    }
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found