I am wondering if there is an elegant way to trim some text but while being HTML tag aware?

我想知道是否有一種優雅的方式來修剪一些文本但是在識別HTML標簽的同時?

For example, I have this string:

例如,我有這個字符串:

$data = 'some title text here that could get very long';

And let's say I need to return/output this string on a page but would like it to be no more than X characters. Let's say 35 for this example.

並且假設我需要在頁面上返回/輸出此字符串,但希望它不超過X個字符。讓我們說35這個例子。

Then I use:

然后我用:

$output = substr($data,0,20);

But now I end up with:

但現在我最終得到:

some title text here that

which as you can see the closing strong tags are discarded thus breaking the HTML display.

正如您所看到的那樣,關閉強標簽將被丟棄,從而打破HTML顯示。

Is there a way around this? Also note that it is possible to have multiple tags in the string for example:

有沒有解決的辦法?另請注意,可以在字符串中包含多個標記,例如:

some text here and here

3 个解决方案

#1

3

A few mounths ago I created a special function which is solution for your problem.

幾個mounths之前我創建了一個特殊功能,它可以解決您的問題。

Here is a function:

這是一個功能:

function substr_close_tags($code, $limit = 300)

{

if ( strlen($code) <= $limit )

{

return $code;

}

$html = substr($code, 0, $limit);

preg_match_all ( "#

foreach($result[1] AS $key => $value)

{

if ( strtolower($value) == 'br' )

{

unset($result[1][$key]);

}

}

$openedtags = $result[1];

preg_match_all ( "#([a-zA-Z]+)>#iU", $html, $result );

$closedtags = $result[1];

foreach($closedtags AS $key => $value)

{

if ( ($k = array_search($value, $openedtags)) === FALSE )

{

continue;

}

else

{

unset($openedtags[$k]);

}

}

if ( empty($openedtags) )

{

if ( strpos($code, ' ', $limit) == $limit )

{

return $html."...";

}

else

{

return substr($code, 0, strpos($code, ' ', $limit))."...";

}

}

$position = 0;

$close_tag = '';

foreach($openedtags AS $key => $value)

{

$p = strpos($code, (''.$value.'>'), $limit);

if ( $p === FALSE )

{

$code .= (''.$value.'>');

}

else if ( $p > $position )

{

$close_tag = ''.$value.'>';

$position = $p;

}

}

if ( $position == 0 )

{

return $code;

}

return substr($code, 0, $position).$close_tag."...";

}

這是演示:http://sandbox.onlinephpfunctions.com/code/899d8137c15596a8528c871543eb005984ec0201(單擊“執行代碼”以檢查其工作原理)。

#2

0

Using @newbieuser his function, I had the same issue, like @pablo-pazos, that it was (not) breaking when $limit fell into an html tag (in my case
at the r)

使用@newbieuser他的功能,我遇到了同樣的問題,比如@ pablo-pazos,當$ limit落入html標簽時(在我的情況下,在r處)它是(不)破壞的

Fixed with some code

修復了一些代碼

if ( strlen($code) <= $limit ){

return $code;

}

$html = substr($code, 0, $limit);

//We must find a . or > or space so we are sure not being in a html-tag!

//In my case there are only

//If you have more tags, or html formatted text, you must do a little more and also use something like http://htmlpurifier.org/demo.php

$_find_last_char = strrpos($html, ".")+1;

if($_find_last_char > $limit/3*2){

$html_break = $_find_last_char;

}else{

$_find_last_char = strrpos($html, ">")+1;

if($_find_last_char > $limit/3*2){

$html_break = $_find_last_char;

}else{

$html_break = strrpos($html, " ");

}

}

$html = substr($html, 0, $html_break);

preg_match_all ( "#

......

#3

-3

substr(strip_tags($content), 0, 100)

substr(strip_tags($ content),0,100)

php 保留html,PHP substr但保留HTML標簽?相关推荐

  1. 用java编写保留两位小数_Java保留两位小数的几种写法总结

    摘要:这篇Java开发技术栏目下的"Java保留两位小数的几种写法总结",介绍的技术点是"Java保留两位小数.保留两位小数.两位小数.Java.小数.总结", ...

  2. oracle保留六位小数,oracle 小数保留位数

    select to_char(0202+1,'FM0000') from dual 输出结果0203 select to_char(59.623,'FM0000') from dual 输出结果:00 ...

  3. C++输出保留两位有效数字和保留小数点后两位

    C++输出保留两位有效数字和保留小数点后两位 #include <iomanip> //保留小数点后2位 cout << setiosflags(ios::fixed) < ...

  4. 计算机磁盘有系统保留,win10重做系统保留桌面文件-win10重置系统选择保留文件时,会保留安装在系统盘以及其他磁盘的软件吗?...

    win10系统无法直接修复怎么办,桌面上有重要文件,除了重装系统还有办法能保住桌面文件吗 ? 只要你不乱动,桌面文件当然可以找回. 前提是按照规范操作,找一个能上网的电脑,找一个空白优盘.远程给你制作 ...

  5. 前端保留两位有效数字_js保留两位小数方法总结

    本文是小编针对js保留两位小数这个大家经常遇到的经典问题整理了在各种情况下的函数写法以及遇到问题的分析,以下是全部内容: 一.我们首先从经典的"四舍五入"算法讲起 1.四舍五入的情 ...

  6. php保留一位小数_PHP保留小数的相关方法

    结合一些小例子 $num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 但是这个如果没有两位小数也不会"两位精度" echo round($num,2) ...

  7. java拼接sql保留两位小数_SQL 保留两位小数的实现方式

    问题: 客户要求,跟金额相关的数据,打印出来要保留两位小数,比如:13.2/13.200要显示为13.20: 分析: 首先查看数据库中的数据定义,均为decimal(12,2) ,直接通过数据库查询的 ...

  8. java.math.BigDecimal保留两位小数,保留小数,精确位数

    http://blog.csdn.net/yuhua3272004/article/details/3075436 使用java.math.BigDecimal工具类实现   java保留两位小数问题 ...

  9. 战双服务器信息保留多久,双线服务器里保留内测时的等级吗

    2007-05-27 公测时,内测号保留吗???如果保 本公会长入住诛仙网通一<天狼>痴心等了好就的 喜欢诛仙的.想和团队一起玩的 喜欢组织的 盼望好久的朋友们 欢迎加入本公会<蝶舞 ...

  10. python round保留小数位_Python-其他-round()保留小数位时遇到的问题

    最近有一个需求,原有整数计算,改成小数计算,保留一位小数. 于是按照需求,将数据结构由 int 改为 float ,计算时采用round()方法来保留小数位. 第一版代码如下: a = 0.10000 ...

最新文章

  1. 【C++】【十一】二叉树递归遍历与非递归遍历的实现及思路
  2. 五大HR所不认同的跳槽理由(转)
  3. 现代密码学2.2、2.3--由“一次一密”引出具有完美安全的密码方案共同缺点
  4. 计算机考到贴吧的试题,2014年全国计算机二级VB上机考试试题七
  5. 微信模版消息 touser 能否多个 群发
  6. python3 整除_python如何整除
  7. --eval mongo_这就是为什么您的read-eval-print-loop如此惊人的原因
  8. 向量空间模型(VSM)--用于计算文本相似度
  9. Java_Dubbo视频教程-雷丰阳-专题视频课程
  10. IEEE 浮点数编码规则
  11. 使用Spire组件抛出异常The type initializer for 'spr857' threw an exception
  12. C语言求最小公倍数和最大公约数三种算法(经典)----ACM
  13. 缺少微信小程序测试经验?这篇文章带你从0开始
  14. 案例|山东省中医院基于ZABBIX构建网络设备监控预警平台
  15. 《A Novel Approach to 3-D Gaze Tracking Using Stereo Cameras》论文阅读
  16. python传输视频文件_Python视频传输
  17. C/C++:实现精灵游戏
  18. Spring aop开发步骤
  19. 如何在微信公众号的文章推送中展示bilibili的视频
  20. 初识大数据与Python语言——学习笔记

热门文章

  1. Java之IO,BIO,NIO,AIO知多少?【请按正文网址阅读】
  2. MySQL使用EXPLAIN查看SQL的执行计划
  3. Spring Cloud Alibaba Sentinel之服务降级篇
  4. MyCat分片规则之取模范围分片
  5. Set集合之HashSet添加的数据是如何保证不重复的
  6. 源码专题之spring设计模式:策略模式、原型模式、模板模式
  7. 【Amaple教程】4. 组件
  8. Grafana 安装和使用
  9. MySql 5.6 Packet for query is too large
  10. HighCharts (web 页面的图表框架)