开发者

PHP Determining Font Size based off of width

开发者 https://www.devze.com 2023-02-20 15:25 出处:网络
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 describe

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 tex开发者_开发问答t 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


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:

  • The operating system's text size level
  • The browser's zoom level
  • The availability of the font used on the client machine

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)


This answer (by Pekka, see his response as well):

PHP Accurate Font Width

Suggests imagettfbbox, if you're using a truetype font.

0

精彩评论

暂无评论...
验证码 换一张
取 消