简体   繁体   中英

PHP x lines than stop

How can i only echo 5 lines?

<?php
$text = Here i have a long long long text about 20 lines;
echo $text; 
?>

So and i want something like that.-> TEXTLINE 1 TEXTLINE 2 TEXTLINE 3 TEXTLINE 4 TEXTLINE 5

And than stop.

Explode the string to array, then loop through the array until last line you want to print. Printh each line while looping the array.

Code for the mentioned text of Harri (at least this would be my approach):

$strings = explode(" ", $text);
for($i=0; $i<$your_lines; $i++) {
    echo $strings[$i] . " ";
}

当您需要行(不是单词)时使用

$lines = explode(PHP_EOL, $text);

Assuming the output is a web browser , then this is a display issue, since what you are referring to as "line" depends on the width/height of the container. (It's not very clear what you try to ask)

You can set a width and height on a div using css and use overflow hidden on that to achieve a result like the one you want

demo

http://jsfiddle.net/g5Y5c/

.mydiv {width:100px;height:100px;overflow:hidden;}

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