Reputation: 4423
I have static width of an element, and an unknown string length which is inserted by the user. I'm trying to keep javascript out otherwise this problem would be solved via using jQuery method described in other posts.
I'm generating a static page with php that won't change and I'm trying to calculate the size of the text in order to fit inside the div.
Is this possible with only having the following: - String Length - Width of Container - Server side with PHP
and if so could you please lend me a hand in finding the correct solution?
Cheers
Upvotes: 1
Views: 1726
Reputation: 449475
If this is about an HTML element, you will not be able to determine the width in PHP with 100% reliability. You would have to actually render the element in the client browser to find it out, and that is possible only using JavaScript.
Plus there are many factors on client side that can play into the result:
You may be best off manually determining the element's width in an average web browser with normal zoom (or if it's dynamic text, maybe use what @Jared proposes)
Upvotes: 2
Reputation: 49208
This answer (by Pekka, see his response as well):
Suggests imagettfbbox, if you're using a truetype font.
Upvotes: 1