Как вывести начальную часть заданного текста ?

ruskam

Новичок
Как вывести начальную часть заданного текста ?

есть переменная, в которой задан любой текст, скажем

$text='Please note that pattern is a regular expression. If you want to split on any of the characters which are considered special by regular expressions, you'll need to escape them first. If you think split() (or any other regex function, for that matter) is doing something weird, please read the file regex.7, included in the regex/ subdirectory of the PHP distribution. It's in manpage format, so you'll want to do something along the lines of man /usr/local/src/regex/regex.7 in order to read it.';
---------
Вопрос: Есть ли ф-я, которая может вывести первые 12 слов этого текста ?. Если нету, то как это можно попроще организовать...
 

WP

^_^
PHP:
$text = 'Please note that pattern is a regular expression If you want to split on any of the characters which are considered special by regular expressions, you\'ll need to escape them first. If you think split() (or any other regex function, for that matter) is doing something weird, please read the file regex.7, included in the regex/ subdirectory of the PHP distribution. It\'s in manpage format, so you\'ll want to do something along the lines of man /usr/local/src/regex/regex.7 in order to read it.';
preg_match('~(?:\w+\W+){0,12}~',$text,$matches);
var_dump($matches[0]);
 

MadGreen

meninweb
PHP:
    $text = trim(substr($text, 0, $simbol));
    $text = substr($text, 0, strrpos($text, ' '));
    $text .= '...';
$simbol количество символов - после которого оставшиеся слова обрубаются по пробелу (чтобы не резало слово)...
если тебе приспичело в качестве параметра передавать количество слов - по пробелам в тексте высчитай их и посчитай занимаемое кол-во символов...
 
Сверху