简体   繁体   English

多段

[英]Multiple paragraphs

I'm using php and have a string, say around 600 characters. 我使用的是php,有一个字符串,大约600个字符。 I want to divide the text into multiple paragraphs of equal height on the screen, so that I end up with two blocks from the single string, for example: 我想将文本分成屏幕上等高的多个段落,这样我最后得到了单个字符串中的两个块,例如:

<div class="firstParagraph">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam consectetur venenatis blandit. Praesent vehicula, libero non pretium vulputate, lacus arcu facilisis lectus, sed feugiat tellus nulla eu dolor. Nulla porta bibendum lectus quis
</div>

<div class="secondParagraph">
euismod. Aliquam volutpat ultricies porttitor. Cras risus nisi, accumsan vel cursus ut, sollicitudin vitae dolor. Fusce scelerisque eleifend lectus in bibendum. Suspendisse lacinia egestas felis a volutpat.
</div>

How do I process that text to achieve this? 如何处理该文本以实现此目的? I'm aware of the standard text manipulators in php but they will split on the number of characters, not the resulting paragraph height (number of lines to render). 我知道php中的标准文本操纵器,但它们将根据字符数而不是结果的段落高度(要渲染的行数)进行分割。

Is there a way to do this? 有没有办法做到这一点?

If you can use CSS3, there is a column-count selector that you can use. 如果可以使用CSS3,则可以使用一个列数选择器。

p {
  -webkit-column-count: 2;
  -mix-column-count: 2;
  column-count: 2;
}

See browser support here 在此处查看浏览器支持

With the query as you have written it, you will get back a table of columns and rows. 使用已编写的查询,您将获得一个包含列和行的表。 For example, you have: 例如,您有:

$query = "SELECT * FROM about"; // this statement will return all columns.
// if you don't need to do this, don't do it for security reasons, 
// and don't waste processing power.
$result = mysql_query($query) or die(mysql_error()); // runs the query
$row = mysql_fetch_array($result); // it's more specific to use mysql_fetch_assoc($result)
// since you will then be able to use values as follows:
// $content = $row['paragraphInfo'];
// also it is shorthand for what you wrote, which would be better written as:
// $row = mysql_fetch_array($result, MYSQL_ASSOC);
echo $row['content']; // as you've written it, this will only echo something if you 
// have a column named 'content'

If this is your goal, to return a 'content' column, and this column is type VARCHAR in your database, you can then use various string methods to break the string up using PHPs substr() method . 如果您的目标是返回“内容”列,并且该列在数据库中的类型为VARCHAR,则可以使用各种字符串方法使用PHP的substr()方法将字符串分解。

The parameters are the original string, a start location and end location, and returns the extracted portion, or false / empty string if the input didn't make sense. 参数是原始字符串,开始位置和结束位置,并返回提取的部分;如果输入没有意义,则返回false /空字符串。 So if you want to just cut the string in half, you could do something like this: 因此,如果您只想将字符串切成两半,则可以执行以下操作:

$content = $row['content'];
$breakpoint = round($content.length / 2); // half of the string length
$first = substr($content, 0, $breakpoint);
$second = sbustr($content, $breakpoint); // no third param means "to the end of the string"

Then you can echo each string as a paragraph: 然后,您可以将每个字符串作为段落进行回显:

echo "<p>" . $first . "</p>";
echo "<p>" . $second . "</p>";

or any other element, for that matter. 或任何其他元素。

I don't know what version of PHP you are using, and I know that some developers are stuck with archaic servers or versions of PHP and do not have access or permission to upgrade, but if you can update your PHP version to 5.5 or 5.6, they are the latest versions (as of this writing). 我不知道您使用的是哪个版本的PHP,并且我知道有些开发人员只能使用过时的服务器或PHP版本,并且没有访问权限或升级权限,但是如果您可以将PHP版本更新为5.5或5.6, ,它们是最新版本(在撰写本文时)。 The mysql_ methods are deprecated as of PHP 5.5 and you can update to change your methods to use mysqli_ methods, which are virtually the same, but take advantage of the improved functionality of more recent versions of MySQL (hence the 'i' - for improved). 从PHP 5.5开始不推荐使用mysql_方法,您可以进行更新以将您的方法更改为使用mysqli_方法,这实际上是相同的,但是要利用MySQL最新版本的改进功能(因此,“ i”可以)。 HOWEVER, I STRONGLY recommend studying PDO, which stands for PHP Data Objects. 但是,我强烈建议您学习PDO,它代表PHP数据对象。 This is a system agnostic library of classes that work no matter which database you use, and if you ever have to switch database systems, you only have to change one line for your database, as well as new credentials for the different system. 这是一个与系统无关的类库,无论您使用哪个数据库,该类都起作用,并且如果必须切换数据库系统,则只需更改数据库的一行,以及更改其他系统的新凭据即可。 Also, it has built in functionality such as parameter binding and database sanitization through prepared statements. 此外,它还具有内置功能,例如通过准备好的语句进行参数绑定和数据库清理。

Notes: See round() on php.net 注意: 请参见php.net上的round()

See MySQL Improved Extension 参见MySQL改进的扩展

See PDO on php.net 参见php.net上的PDO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM