Solved

How can I calculate the width a string will take up when displayed in a browser?

Posted on 2008-10-08
3
571 Views
Last Modified: 2013-12-12
I am creating a report writer and need to calculate string widths so that I create text areas that grow.
I can use offsetWidth with javascript to find the width but thats once it has already appeared in the browser, I was wondering if php can accurately calculate this before I echo it to the screen?
0
Comment
Question by:nuSteven
3 Comments
 
LVL 82

Expert Comment

by:hielo
Comment Utility
you can obtain the lenght of the string (in characters):
$str="Hello World";
echo strlen($str);

but the server doesn't know what resolution the user has, so you will not be able to get resolution information on the server. You will need to compute that on the browser via javascript.
0
 
LVL 2

Accepted Solution

by:
kanchan_karjee earned 500 total points
Comment Utility
You can store the resolution in cookies, there should be some page which comes before your report page, get the resolution there and store it in cookies, so the resolution will be available to you before you render the report page.  
0
 
LVL 4

Expert Comment

by:kingmonty
Comment Utility
Unless you're using a fixed width font, you won't know the exact length of the string until it hits the browser - even if a users resolution is stipulated, a user can override the font size. So your best bet is to use your javascript. You can hide the element until it's rendered correctly, then you can just show it...
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

8 Experts available now in Live!

Get 1:1 Help Now