PHP to Calculate Text/HTML Ratio

9 replies
Hi,

I am currently building an online application using PHP, where I need to calculate the text to html ratio of a website.

Anyone has a good PHP class or script to calculate this?

Thanks!
#calculate #php #ratio #text or html
  • Profile picture of the author Earnie Boyd
    I see a few examples of use after doing a search. I don't see code examples though.

    I did find
    javascript:alert('text/html = '+(b=document.body).innerText.length/b.innerHTML.length);void(0);

    in a comment at Search Engine Optimization: Textarea and HTML comments - CodingForums.com
    Signature
    {{ DiscussionBoard.errors[5930301].message }}
  • Profile picture of the author sjmalta
    Unfortunately I'm not proficient in Javascript :/

    I don't want to cut the possibility of doing Javascript - but I need a better tutorial in order to implement it ...

    Anyone with a better solution?
    {{ DiscussionBoard.errors[5930409].message }}
    • Profile picture of the author Brandon Tanner
      What exactly do you mean by "text to HTML ratio"?
      Signature

      {{ DiscussionBoard.errors[5931318].message }}
      • Profile picture of the author SteveJohnson
        Originally Posted by Brandon Tanner View Post

        What exactly do you mean by "text to HTML ratio"?
        The ratio of the number of text characters that the browser actually displays to the total number of characters in the HTML doc. Essentially, the number of characters between the closing tag symbol > and the opening tag symbol.
        Signature

        The 2nd Amendment, 1789 - The Original Homeland Security.

        Gun control means never having to say, "I missed you."

        {{ DiscussionBoard.errors[5931579].message }}
        • Profile picture of the author KirkMcD
          Originally Posted by SteveJohnson View Post

          Essentially, the number of characters between the closing tag symbol > and the opening tag symbol.
          And why would you want to know this?

          Any way load the page into a string and save its size.
          Then use the strip_tags function to remove the html and then check its size.
          Compare as you see fit.
          {{ DiscussionBoard.errors[5932016].message }}
        • Profile picture of the author Brandon Tanner
          Originally Posted by SteveJohnson View Post

          The ratio of the number of text characters that the browser actually displays to the total number of characters in the HTML doc. Essentially, the number of characters between the closing tag symbol > and the opening tag symbol.
          Gotcha. I guess a better question then would be why would anyone ever need to know this?
          Signature

          {{ DiscussionBoard.errors[5933979].message }}
  • Profile picture of the author sjmalta
    I am currently building a tool to analyse this as it is an important factor for SEO.

    jassmee, I am not looking for the actual size in pixels - I need the ratio of html characters to content characters.

    KirkMcD is the one who got the closest ... thought there might be something ready for use out there though!
    {{ DiscussionBoard.errors[5936182].message }}
  • Profile picture of the author traderookie
    You can try something below, if your real intention is to find out keyword density.

    $content = strip_tags($html_content);
    $len = strlen($content);

    foreach($keyword_array as $keyword)
    {
    $found = substr_count($content, $keyword);
    $keyword_density[$keyword] = ($found / $content);
    }

    Well, I really don't think keyword density is really important now, since Google just changed its SEO algo again.
    {{ DiscussionBoard.errors[5936237].message }}
  • Profile picture of the author sjmalta
    I was searching for Content/HTML ratio - followed KirkMcD's suggestion and it worked.

    Yeps, keyword density is next - traderookie's code gave errors. Will see how to go about it.

    I am sure both factors still affect Google's algorithm.
    {{ DiscussionBoard.errors[5936619].message }}

Trending Topics