Solved

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

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

622 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