$text =~ s{([^\0-\377])}{'&#'.ord($1).';'}ge; [download]
In reply to use HTML::Entities to encode only characters >= 256? by pc88mxer