简体   繁体   中英

Multiple paragraphs

I'm using php and have a string, say around 600 characters. 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).

Is there a way to do this?

If you can use CSS3, there is a column-count selector that you can use.

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 .

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. 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). 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). HOWEVER, I STRONGLY recommend studying PDO, which stands for PHP Data Objects. 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

See MySQL Improved Extension

See PDO on php.net

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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