2
4

2 回答 2

1

You can use:

$body = preg_replace('/^(.{0,299}\w)\b.*/su', '$1…', $body);

\w before \b ensures we don'e add ellipsis after a non-word character

于 2016-01-26T20:48:09.547 回答
1

Here is your fixed approach:

$body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu congue ex. Nunc sem arcu, fermentum vel feugiat quis, consequat nec enim. Quisque et pulvinar velit, et laoreet justo. Integer quis sapien ac turpis mattis lobortis at at metus. Vestibulum euismod turpis odio, id luctus quam pharetra, at, et. Sed finibus, nunc at ultricies posuere, dui mauris aliquet quam, eget aliquet ligula libero a turpis. Pellentesque eu diam sodales, sollicitudin leo et, sagittis magna. Donec feugiat, velit quis condimentum porttitor, enim sapien varius elit, sit amet pretium risus turpis vitae massa. Sed ac ligula sit amet lorem scelerisque tristique a id ex. Nullam maximus tincidunt magna, vel molestie lectus tempus non. Sed euismod placerat ultricies. Morbi dapibus augue ut odio faucibus, vel maximus nisl pharetra. Aliquam hendrerit dolor in ipsum pharetra, eget tincidunt lacus ultrices.";

$line = $body;
if(strlen($body) > 300 && preg_match('/^(.{1,300})(?!\w)\b\p{P}*/su', $body, $match)) {
    $line = trim($match[1]) . "…";
}
echo $line;

See eval.in demo

As I noted in the comments, you can match the punctuation (optionally, with \p{P}*), but I forgot that \b can match both trailing and leading word boundary. By restricting the \b with the negative lookahead (?!\w) (like (?!\w)\b) we only match the trailing word boundary.

Besides, the capturing group ((...)) is added to the pattern so that we only capture into Group 1 the string with trailing punctuation trimmed out, and the value can be accessed with $match[1].

于 2016-01-26T21:34:24.060 回答