How to get the length of string containing HTML Entities using Perl?

Posted on 2011-04-27
Last Modified: 2013-12-25
I have a string variable containing mix of HTML Entities of Japanese characters, I would like to get the length of the string, and treat each HTML Entity as length of 2, and standard English character as length of 1.

I would like to use regular express for matching the &#xxxxx; counts, how can I get this done?

eg. $str contains abc&#xxxxx;123&#xxxxx;xyz

Normally, I would get the length of 25, but instead, I would like to get length of 11.
Question by:jay28lee
    LVL 25

    Accepted Solution

    You might try:

    $a = $str;  # copy the string
    $a =~ s/&#.*?;/aa/sg;  # swap all your html entities with a two character string
    print length($a);  # return its length

    Open in new window

    LVL 84

    Expert Comment

    print $length=()="abc&#xxxxx;123&#xxxxx;xyz"=~/&.*?;|./g    
    LVL 84

    Expert Comment

    Do you want a length of 11 or 13?

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
    Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now