Solved

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

Posted on 2008-10-08
3
573 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
ID: 22675965
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
ID: 22676055
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
ID: 22676262
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…

813 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

14 Experts available now in Live!

Get 1:1 Help Now