我会把table div tr td不需要的标签全部去除 只留下内容 --------------------------then how about <input ...>??????
Getting the <img> src attribute in 500 bytes first then delete all html tags of the string in 1000 bytes(may be more or less) get first 500 bytes of the sting add <img> tags anywhere you like.Hope it will be easier....
sina blog类似的补全处理代码, 是由ie自动完成的,这个过程必须在客户端,而asp服务端无法直接这么用,所以是要自己编程的,我个人认为,可以把除了<font><span><div><b><i><p><img>等少数几个最没有麻烦的标记之外的所有标记都过滤掉,然后在第500个的位置回头找“<”和“>”,如果先找到">"那就正好不用再处理了,如果先找到"<"那就再往下找到第一个">"出现的位置,截取到这里就好。
/**
* Works like PHP function strip_tags, but it only removes selected tags.
* Example:
* strip_selected_tags('<b>Person:</b> <strong>Salavert</strong>', 'strong') => <b>Person:</b> Salavert
*/ function strip_selected_tags($text, $tags = array())
{
$args = func_get_args();
$text = array_shift($args);
$tags = func_num_args() > 2 ? array_diff($args,array($text)) : (array)$tags;
foreach ($tags as $tag){
if(preg_match_all('/<'.$tag.'[^>]*>(.*)<\/'.$tag.'>/iU', $text, $found)){
$text = str_replace($found[0],$found[1],$text);
}
} return $text;
}
define("PREG_SPLIT_ALL", PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);$text =<<< TEXT
需要从一篇文章中截取前500个字符,做为文章摘要,怎样保证文章中的img标签不被截断?用正则能解决吗?<img src='1.gif'>需要从一篇文章中截取前500个字符,做为文章摘要,怎样保证文章中的img标签不被截断?用正则能解决吗?<img src='1.gif'>
TEXT;$ar = preg_split("/(<\/?[a-z].*?>)/is", $text, -1, PREG_SPLIT_ALL);print_r($ar);
?>
你可以看到,文档被分割成了数块
Array
(
[0] => Array
(
[0] => 需要从一篇文章中截取前500个字符,做为文章摘要,怎样保证文章中的img标签不被截断?用正则能解决吗?
[1] => 0
) [1] => Array
(
[0] => <img src='1.gif'>
[1] => 96
) [2] => Array
(
[0] => 需要从一篇文章中截取前500个字符,做为文章摘要,怎样保证文章中的img标签不被截断?用正则能解决吗?
[1] => 113
) [3] => Array
(
[0] => <img src='1.gif'>
[1] => 209
))
数组元素的第二维分别是匹配到的内容和该内容在原文档中的位置
根据这些信息,你可以很方便的提取你需要的内容通常摘要文字的长度是不包括html标记的
还有要是<img src="1 截取到这样的情况,应该怎么办?
if($v[0][0] != '<') {
if($num+strlen($v[0]) <= 500) {
$num += strlen($v[0]);
}else {
$v[0] = substr($v[0], 0, 500-$num);
}
}
$out .= $v[0];
if($num >= 500) break;
}
这里没有用到$v[1]
--------------------------then how about <input ...>??????
then delete all html tags of the string in 1000 bytes(may be more or less)
get first 500 bytes of the sting
add <img> tags anywhere you like.Hope it will be easier....
$ar = preg_split("/(<\/?[a-z].*?>)/is", $text, -1, PREG_SPLIT_ALL);
另外,我觉得流程这样效率会更高
从文章中截取500个字符,将这500个字符处理下
$ar = preg_split("/(<\/?[a-z].*?>)/is", $text, -1, PREG_SPLIT_ALL);
直接foreach相连
foreach($ar as $v)
{
$out .= $v[0];
}
$text =<<< TEXT
需要从一篇文章中截取前500个字符,做为文章摘要,怎样保证文章中的img标签不被截断?用正则能解决吗?<img src='1.gif'>需要从一篇文章中截取前500个字符,做为文章摘要,怎样保证文章中的img标签不被截断?用正则能解决吗?<img src='1.gif'>
TEXT;$out = 0;
$ar = preg_split("/(<\/?[a-z].*?>)/is", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach($ar as $key=>$val)
{ $out .= $val;
}echo($out);
table tr td ... 表格标记
pre
select option
script
...如果周全,很多标记要特别考虑呀,偶做过这个,烦死了
lz说了,项目需求就是要求图文并茂的