Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-08
3
Medium Priority
?
583 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

688 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