Рассмотрим несколько стандартных PHP функций, при помощи которых можно преобразовать наш текст.
Как в PHP обрезать текст до определенного символа?
Для начала напишем переменную, над которой и будем эксперементировать:
$string = "<p><strong>Richard III</strong> (2 October 1452 – 22August 1485) was King of England for two years, from 1483 until his death in 1485 in the Battle of Bosworth Field. He was the last king of the House of York and the last of the Plantagenet dynasty. His defeat at Bosworth Field, the decisive battle of the Wars of the Roses, is sometimes regarded as the end of the Middle Ages in England. He is the subject of the play<cite>Richard III</cite> by <a href=//en.wikipedia.org/wiki/William_Shakespeare>William Shakespeare.</a>"
Как видите, она содержит теги, от которых было бы хорошо избавиться. Начнем.
Первым делом, уберём все html элементы:
$string = strip_tags($string);
Теперь обрежем его на определённое количество символов:
$string = substr($string, 0, 200);
Затем убедимся, что текст не заканчивается восклицательным знаком, запятой, точкой или тире:
$string = rtrim($string, "!,.-");
Напоследок находим последний пробел, устраняем его и ставим троеточие:
$string = substr($string, 0, strrpos($string, ' ')); $string = $string.'...';
Выводим при помощи стандартного php echo, результат будет такой:
Richard III (2 October 1452 – 22 August 1485) was King of England for two years, from 1483 until his death in 1485 in the Battle of Bosworth Field. He was the last king of the House of York and the…
А теперь весь код целиком:
$string = "<p><strong>Richard III</strong> (2 October 1452 – 22August 1485) was King of England for two years, from 1483 until his death in 1485 in the Battle of Bosworth Field. He was the last king of the House of York and the last of the Plantagenet dynasty. His defeat at Bosworth Field, the decisive battle of the Wars of the Roses, is sometimes regarded as the end of the Middle Ages in England. He is the subject of the play<cite>Richard III</cite> by <a href=//en.wikipedia.org/wiki/William_Shakespeare>William Shakespeare.</a>" $string = strip_tags($string); $string = substr($string, 0, 200); $string = rtrim($string, "!,.-"); $string = substr($string, 0, strrpos($string, ' ')); $string = $string.'...';
Повторим – мы использовали strip_tags, substr, rtrim и strrpos для достижения такого результата. И, что самое главное, данный скрипт можно улучшать и менять под свои задачи.
В своё время он помогал мне выводить “описание анонса” для товаров, формируя его из детального текста 🙂