Y
Y
yischyk2021-07-14 10:07:44
PHP
yischyk, 2021-07-14 10:07:44

How to crop text line by line?


This is the first line of text.

And this is the second line.

And here is the third line.


How can I get the first 3 lines using PHP methods (along with a wrap, for example)

This is the first line of text.

And this is the second line.


If the number of lines and the text itself are unknown?

Copal in the office. I did not find it on the docks, on the forums, I was looking badly. I will be grateful!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-07-14
@yischyk

<?php
$text = 'Это первая строка текста.
А это вторая строка.
А здесь третья строка.';

// split to rows
$rows=explode(PHP_EOL, $text);

//get first 2 rows
$first2rows = array_slice($rows, 0, 2);

//implode to new text
$newtext = implode(PHP_EOL,$first2rows);

var_export($newtext);

Run PHP here

Y
yischyk, 2021-07-14
@yischyk

I found it only using the CSS method (maybe someone will need it). CSS suits me too, but the implementation in PHP is interesting How to cut text by the number of lines?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question