1 回答
TA贡献1808条经验 获得超4个赞
在裁剪之前使用 PHP 的strip_tags()进行此操作。
<?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
或者与一些更高级的用法相同
<?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 关注
- 68 浏览
添加回答
举报