Solved

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

Posted on 2008-10-08
3
574 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 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