lxc521 发表于 2007-04-28 13:55

用substr提取字符遇到图片,怎么处理

比如我提取出一篇文章时,只要用到前面的50个字
我用:
$m=substr($article,0,100)
但当遇到有图片或其它内容时,却不能跳过取下面的字

该怎么处理这个问题啊

HonestQiao 发表于 2007-04-28 14:53

当然先用正则分析去掉无用的部分啦。

笨狼追风 发表于 2007-04-28 15:15

strip_tags();

unspace 发表于 2007-04-28 16:11

一般做文字字数限制或分页的或采集的
都要先把图片这类的东西去掉

lxc521 发表于 2007-04-28 18:54

原帖由 笨狼追风 于 2007-4-28 15:15 发表
strip_tags();

我用了这个没起作用

olever 发表于 2007-04-29 07:53

回复 5楼 lxc521 的帖子

把你的那段代码发上来看看,我也是用的strip_tags(); 来弄得。

HonestQiao 发表于 2007-04-29 09:05


<?php
$summary = url_to_link($summary);
$summary = strip_tags_except($summary, array('a'), FALSE);
?>
<?php
function strip_tags_except($text, $allowed_tags, $strip=TRUE) {
if (!is_array($allowed_tags))
    return $text;

if (!count($allowed_tags))
    return $text;

$open = $strip ? '' : '&lt;';
$close = $strip ? '' : '&gt;';

preg_match_all('!<\s*(/)?\s*(+)[^>]*>!',
    $text, $all_tags);
array_shift($all_tags);
$slashes = $all_tags;
$all_tags = $all_tags;
foreach ($all_tags as $i => $tag) {
    if (in_array($tag, $allowed_tags))
      continue;
    $text =
      preg_replace('!<(\s*' . $slashes[$i] . '\s*' .
      $tag . '[^>]*)>!', $open . '$1' . $close,
      $text);
}

return $text;
}

function url_to_link($text) {
$text =
    preg_replace('!(^|([^\'"]\s*))' .
      '({2,4}:\/\/[^\s<>"\'()]{4,})!mi',
      '$2<a href="$3">$3</a>', $text);
$text =
    preg_replace('!<a href="([^"]+)[\.:,\]]">!',
    '<a href="$1">', $text);
$text = preg_replace('!([\.:,\]])</a>!', '</a>$1',
    $text);
return $text;
}
?>

lxc521 发表于 2007-05-04 21:47

HonestQiao
我看正则表达式里好像并没有!的介绍
不知道这个在你上面的代码中起什么作用,能否说明一下呢

HonestQiao 发表于 2007-05-05 09:53

原帖由 lxc521 于 2007-5-4 21:47 发表
HonestQiao
我看正则表达式里好像并没有!的介绍
不知道这个在你上面的代码中起什么作用,能否说明一下呢

友情提示:请非特别仔细看手册:
http://cn.php.net/manual/zh/ref.pcre.php

本类函数中所使用的模式极其类似 Perl。表达式应被包含在定界符中,如斜线(/)。任何不是字母、数字或反斜线(\)的字符都可以作为定界符。如果作为定界符的字符必须被用在表达式本身中,则需要用反斜线转义。自 PHP 4.0.4 起,也可以使用 Perl 风格的 (),{},[] 和 <> 匹配定界符。详细解释见模式语法。

substr函数 发表于 2014-11-11 22:36

谢谢 :hug: :hug:
页: [1]
查看完整版本: 用substr提取字符遇到图片,怎么处理