1 回答

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
在裁剪之前使用 PHP 的strip_tags()進(jìn)行此操作。
<?php
$str = 'lorem ipsum is simply dummy text of the printing and typesetting industry <img src="data/uploads/image.jpg" class="img-responsive" style="width: 100%" /> and some more text behind the image.... ';
$pre = strip_tags($str);
$crop = substr($pre, 0, 100);
echo $crop;
// Output:
// lorem ipsum is simply dummy text of the printing and typesetting industry and some more text behind
或者與一些更高級(jí)的用法相同
<?php
$str = 'lorem ipsum is simply dummy text of the printing and typesetting industry <img src="data/uploads/image.jpg" class="img-responsive" style="width: 100%" /> and some more text behind the image.... ';
echo crop($str, 100, '... (read more)', true, true);
function crop($content, $maxCharacters, $append = '...', $respectWordBoundaries = false, $stripTags = false)
{
? ? if ($stripTags) {
? ? ? ? $content = strip_tags($content);
? ? }
? ? if ($maxCharacters) {
? ? ? ? if (mb_strlen($content, 'utf-8') > abs($maxCharacters)) {
? ? ? ? ? ? $truncatePosition = false;
? ? ? ? ? ? if ($maxCharacters < 0) {
? ? ? ? ? ? ? ? $content = mb_substr($content, $maxCharacters, null, 'utf-8');
? ? ? ? ? ? ? ? if ($respectWordBoundaries) {
? ? ? ? ? ? ? ? ? ? $truncatePosition = strpos($content, ' ');
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? $content = $truncatePosition ? $append . substr($content, $truncatePosition) : $append . $content;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? $content = mb_substr($content, 0, $maxCharacters, 'utf-8');
? ? ? ? ? ? ? ? if ($respectWordBoundaries) {
? ? ? ? ? ? ? ? ? ? $truncatePosition = strrpos($content, ' ');
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? $content = $truncatePosition ? substr($content, 0, $truncatePosition) . $append : $content . $append;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? return $content;
}
- 1 回答
- 0 關(guān)注
- 101 瀏覽
添加回答
舉報(bào)