RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 859610
Accepted
Linne
Linne
Asked:2020-07-24 19:12:41 +0000 UTC2020-07-24 19:12:41 +0000 UTC 2020-07-24 19:12:41 +0000 UTC

从 html 中获取图像和部分文本

  • 772

你需要一个正则表达式来获取文章的第一张图片和一段300个字符的文本来构成文章的公告。

以一篇文章为例:

$content = '<img src="files/1.jpg" alt="" width="200px" />
<img src="files/2.jpg" alt="" width="200px" />
<img src="files/3.jpg" alt="" width="200px" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>';

有必要,我们只输出,比方说:

$content = '<img src="files/1.jpg" alt="" width="200px" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...</p>';

非常希望输出已经带有 html 标记,如果文本被截断,则放置 <...> 和结束标记。

我真的希望得到帮助,我只是一个初学者编码器。

这是代码,但它只输出图像:

while ($row = $results->fetchArray()) {

    preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $row['content'], $image);

    echo 'id: ' . $row['id'] . '<br /> Дата и время: ' . $row['datetime'] . '<br /> Заголовок: ' . $row['title'] . '<br /> Категория: <a href="?category=' . $row['category'] . '">' . $row['category'] . '</a><br /> Контент: ' . $image['src'] . '<br />
        <a href="?article=' . $row['id'] . '">Читать</a>
        <br />-------------------------------------------------<br />';
}
php
  • 4 4 个回答
  • 10 Views

4 个回答

  • Voted
  1. vp_arth
    2020-07-24T19:36:22Z2020-07-24T19:36:22Z

    这种工作的最佳正则表达式是普通的 DOM 解析器。

    $content = '<img src="files/1.jpg" alt="" width="200px" />
    <img src="files/2.jpg" alt="" width="200px" />
    <img src="files/3.jpg" alt="" width="200px" />
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>';
    
    $dom = new DomDocument();
    $dom->loadHTML($content);
    $firstImage = $dom->getElementsByTagName('img')->item(0);
    $src = $firstImage->attributes->getNamedItem('src')->nodeValue;
    echo <<<IMG
    <img src="{$src}" />
    IMG;
    
    $text = $dom->textContent;
    $trunc300 = substr($text, 0, strpos($text, ' ', 300));
    echo <<<HTML
    <p>
      {$trunc300}
    </p>
    HTML;
    
    • 2
  2. Walfter
    2020-07-24T19:26:01Z2020-07-24T19:26:01Z

    为什么只是普通的?

    例如,它可以是这样的

    $content = '<img src="files/1.jpg" alt="" width="200px" />
    <img src="files/2.jpg" alt="" width="200px" />
    <img src="files/3.jpg" alt="" width="200px" />
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>';
    
    $dom = new DOMDocument;
    $dom->loadHTML($content);
    $imgs = $dom->getElementsByTagName('img');
    $src = $imgs[0]->getAttribute('src');
    $width = $imgs[0]->getAttribute('width');
    echo "<img src=\"$src\" width=\"$width\">";
    
    • 1
  3. Best Answer
    user285292
    2020-07-24T19:56:30Z2020-07-24T19:56:30Z

    如果您是自行车迷,那么您可以这样做:)

    preg_match('~<img.+/>\n?~', $content, $matches);
    
    $text = preg_replace_callback('~<p>(.+)</p>~', function ($m) {
        return '<p>'.substr($m[1], 0, strpos($m[1], ' ', 300)).'...</p>'; 
    }, preg_replace('~<img.+\n~', '', $content));
    
    echo $matches[0] . $text;
    
    • 1
  4. mazzy
    2020-07-24T21:51:27Z2020-07-24T21:51:27Z

    同意以前的回答者 - 不要使用正则表达式来解析 html。

    我只想补充一件事:正则表达式不是魔杖,也不是灵丹妙药。正则表达式是一种常见的有限状态机,一种分配内存用于存储小计的程序,循环执行操作,有时甚至使用递归。

    正则表达式越复杂,它工作的时间就越长,消耗的内存就越多。

    因此(如果你被正则表达式卡住而根本无法使用 DOM),通常最好使用多次传递。在您的情况下,第一遍最好找到文本,从图像开始直到并包括文本。并在第二次通过时,从中间结果中剔除多余的部分。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5