簡體   English   中英

多段

[英]Multiple paragraphs

我使用的是php,有一個字符串,大約600個字符。 我想將文本分成屏幕上等高的多個段落,這樣我最后得到了單個字符串中的兩個塊,例如:

<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>

如何處理該文本以實現此目的? 我知道php中的標准文本操縱器,但它們將根據字符數而不是結果的段落高度(要渲染的行數)進行分割。

有沒有辦法做到這一點?

如果可以使用CSS3,則可以使用一個列數選擇器。

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

在此處查看瀏覽器支持

使用已編寫的查詢,您將獲得一個包含列和行的表。 例如,您有:

$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'

如果您的目標是返回“內容”列,並且該列在數據庫中的類型為VARCHAR,則可以使用各種字符串方法使用PHP的substr()方法將字符串分解。

參數是原始字符串,開始位置和結束位置,並返回提取的部分;如果輸入沒有意義,則返回false /空字符串。 因此,如果您只想將字符串切成兩半,則可以執行以下操作:

$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"

然后,您可以將每個字符串作為段落進行回顯:

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

或任何其他元素。

我不知道您使用的是哪個版本的PHP,並且我知道有些開發人員只能使用過時的服務器或PHP版本,並且沒有訪問權限或升級權限,但是如果您可以將PHP版本更新為5.5或5.6, ,它們是最新版本(在撰寫本文時)。 從PHP 5.5開始不推薦使用mysql_方法,您可以進行更新以將您的方法更改為使用mysqli_方法,這實際上是相同的,但是要利用MySQL最新版本的改進功能(因此,“ i”可以)。 但是,我強烈建議您學習PDO,它代表PHP數據對象。 這是一個與系統無關的類庫,無論您使用哪個數據庫,該類都起作用,並且如果必須切換數據庫系統,則只需更改數據庫的一行,以及更改其他系統的新憑據即可。 此外,它還具有內置功能,例如通過准備好的語句進行參數綁定和數據庫清理。

注意: 請參見php.net上的round()

參見MySQL改進的擴展

參見php.net上的PDO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM