以下代码段从显示为预告片的节点的 body 字段中删除所有标签。也就是说,它从body字段创建的原始teaser中剥离标签,注意不要从CCK等模块添加到teaser的额外字段中剥离标签。

第 1 步(共 1 步)

在您的 template.php 中,将以下内容添加到节点预处理函数中:

function THEME_preprocess_node(&$vars, $hook) {
  // Strip tags from teaser
  if ($vars['teaser']) {
    // $coreteaser is the teaser without extra cck fields
    $coreteaser = $vars['node']->content['body']['#value'];
    // Make sure there is content to strip tags from
    if ($coreteaser) {
      $teaser = $vars['content'];
      // Calculate position of $coreteaser in $teaser
      $start = strpos($teaser, $coreteaser);
      // Calculate length of core teaser with tags
      $length = strlen($coreteaser);
      // Strip tags from $coreteaser
      $replacement = strip_tags($coreteaser);
      // Replace corresponding part of $teaser with stripped $coreteaser
      $vars['content'] = substr_replace($teaser, $replacement, $start, $length);
    }
  }
}

笔记

注释

tsssystems 的图片

道具 sheena_d 将我指向此页面和http://php.net/manual/en/function.strip-tags.php通过将来自 php.net 页面的 Steve 的 strip_only 函数与此页面上的代码相结合,您可以有选择地从预告片中删除 html 标签。我需要它来删除 img 标签。我在时事通讯的右边框上有一个又长又窄的图形。预告片副本的时间要长得多,并最终与除了它原本打算使用的预告片之外的其他预告片接壤。这是像魅力一样工作的代码:

<?php

/* Strip only selcted tags in THEME_preprocess_node function below */
function strip_only($str, $tags) {
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
    return $str;
}

/* Strip only selected tags from teasers - in this case, just img tags */
function THEME_preprocess_node(&$vars, $hook) {
  // Strip tags from teaser
  if ($vars['teaser']) {
    // $coreteaser is the teaser without extra cck fields
    $coreteaser = $vars['node']->content['body']['#value'];
    // Make sure there is content to strip tags from
    if ($coreteaser) {
      $teaser = $vars['content'];      
      // Calculate position of $coreteaser in $teaser
      $start = strpos($teaser, $coreteaser);
      // Calculate length of core teaser with tags
      $length = strlen($coreteaser);
      // Strip selected tags only from $coreteaser
      $replacement = strip_only($coreteaser, '<img>');
      // Replace corresponding part of $teaser with stripped $coreteaser
      $vars['content'] = substr_replace($teaser, $replacement, $start, $length);
    }
  }
}

?>
Observer123的图片

请给我一个示例,如何使用此功能仅去除粗体和斜体标签?

Skelly1983 的图片

要去除粗体和斜体标签,只需更改 $replacement 字符串,使其与此相同:

 // Strip selected tags only from $coreteaser
      $replacement = strip_only($coreteaser, '<strong> <em> <b> <i>');

b 和 i 标签包含 bin 只是为了防止用户仍然使用粗体和斜体的旧方法。

年轻的照片

我用 Adaptive 主题尝试过这个,但似乎我错过了一些东西,因为我似乎无法让它工作。
我正在使用内联图像,因为我的内容是一个节点而不是视图中的一个字段,而且我还没有使用 CKEditor。

任何我可能会出错的想法。

JimmyMoonJam 的图片

我也不明白为什么它对我不起作用。我不使用任何编辑器。还尝试使用 Garland 主题和正常的默认页面内容类型。

最后用这个代码删除了 img 标签,我的朋友告诉我尝试:

print preg_replace('#<img[^>]+>#i', '', $node->content['body']['#value']);

将其放入 node.tpl.php 文件中。

julien.reulos 的图片


/* Strip only selcted tags in THEME_preprocess_node function below */
function strip_only($str, $tags) {
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
    return $str;
}

/* Strip only selected tags from teasers - in this case, embed and iframe */
function YOUR_THEME_preprocess_node(&$vars) {
  if(isset($vars['content']['body'][0]['#markup']) && $vars['teaser']) {
    $vars['content']['body'][0]['#markup'] = strip_only($vars['content']['body'][0]['#markup'], '<iframe> <embed>');
  }
}

弗兰德里的图片

这对我来说适用于 D7 和 Bartik 主题,但我必须通过在 strip_only 函数的 for_each 行之后添加这一行来硬编码我想要剥离的标签 img:

$str = preg_replace('/<img[^>]*>/', '', $str);

将 img 添加到函数调用中的标签列表不起作用,并且它也没有删除它应该删除的 iframe 标签。我还必须将 $vars 更改为 $variables。我不确定 strip-only 功能有什么问题,但其他一切似乎都没问题。

我还决定去掉 div 标签和里面的任何东西,因为我在文章中使用它们的唯一一次是浮动带有标题的图像。最好检查 div 中的 img,然后才匹配结束的 div 标签,但这可以满足我现在的需要。

   $str = preg_replace('/<div.*?<\/div>/', '', $str); /*Remove anything between the nearest opening and closing <div> tags. *Doesn't handle nested divs!* */

罗尔夫迈耶的图片

img 标签在这里不起作用的原因是 strip_only 仅替换标签及其关闭计数器部分。img-tag 具有以下结构:,但 strip_only 搜索<tag /><tag></tag>

此外,对于多个标签,我只得到了标签之间没有空格的结果,即对我不起作用,但确实如此。'<iframe> <div>''<iframe><div>'

Skelly1983 的图片

如果您只想保留某些标签并去除其余标签,即。保留 a、img、strong 等,然后您可以更改:

strip_tags($coreteaser)

到...

strip_tags($coreteaser, '<a> <img> <strong> <em> <b> <i> <span> <div> <h1> <h2>')

该函数可以使用两个参数输入,第一部分定义要剥离的字符串,第二部分是您想要从条纹中排除的标签,这样您仍然可以保持样式。

来自  https://www.drupal.org/node/659458