English 中文(简体)
计算以 PHP 为其宽度的 div 中行的行数
原标题:Calculate number of lines in a div with its width in PHP
  • 时间:2012-05-23 17:13:23
  •  标签:
  • php

PHP 中是否有一种方法来计算字符串给定宽度和给定行距的线条?

For example, if I have this text:
Sed eget lorem lorem. Pellentesque tristique, quam vel fringilla porttitor,
neque elit suscipit nisl, id posuere magna libero congue ante.
= 2 lines

Sed eget lorem lorem. Pellentesque
tristique, quam vel fringilla
porttitor, neque elit suscipit nisl,
id posuere magna libero congue ante.
= 4 lines because the width is not the same

Edit: My question was not clear.
As Jason McCreary said, I want to create a formula (based on font-size, string length, line-height) with PHP that will get me close.

如果有人能派我走正确方向 去做我很感激的事 因为我试图创造那个神奇的公式 却毫无结果

我无法在页面中使用 JavaScript 。

谢谢!

最佳回答

最后,这就是我所做的:

$charWidth = 5.6;
$divWidth = 550;

$wrappedContent = wordwrap($originalContent, ($divWidth / $charWidth), "
");
$explodedLines = explode("
", $wrappedContent); 
$nbOfLine = count($explodedLines);

echo $nbOfLine;

没有必要说这是非常接近的。

问题回答

否。 PHP 是服务器的侧面技术。 它不知道 HTML 生成的结果 。

相反,你可以:

  • Use JavaScript. It s a client side technology and will provide much better results.
  • Create a formula (based on font-size, string length, line-height) with PHP that will get you close.

Update

勾选以下函数 : < a href=" "https://www.php.net/strlen" rel="nofollow noreferr"\\ code>strlen () 和substr_count () 以帮助您启动 PHP 公式。 它们会帮助您使用行长度和行数。 行宽和字体大小应该保持不变 。

只能用 CSS 就可以了

<div style="width: 10em; height: 1.2em; overflow: hidden; text-overflow: ellipsis;">
Now a long woooooooooooooooooooooooooooooooord
</div>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签