lissome
V2EX  ›  PHP

请教 preg_replace() 过滤 html 标签

  •  
  •   lissome · Sep 19, 2016 · 3171 views
    This topic created in 3697 days ago, the information mentioned may be changed or developed.

    文章正文中有大量 html 标签,类似这样的:

    <h1>标题</h1>
    <p>文字文字</p>
    <img xxxx />
    

    需要从文章正文提取摘要,现在用的办法是先过滤掉 html 标签,然后截取前 n 个字

    $patterns = ['/<.+>/', '/&nbsp;/'];
    $replacements = ['', ''];
    
    $text = preg_replace($patterns, $replacements, $html);
    

    但是替换结果是一个空字符串,是匹配了第一个 < 和最后一个 > ?

    请问正则应该怎么写?

    或者有没有其它方法从正文提取摘要?

    4 replies  •  2016-09-20 23:53:16 +08:00
    fahai
    fahai
    Sep 19, 2016❤️1
    1
    可以把<.+>改成<.*?>或者使用 strip_tags
    lissome
    lissome
    OP
    Sep 19, 2016
    2
    strip_tags
    http://php.net/manual/en/function.strip-tags.php

    刚刚我也查到了,谢谢!
    @fahai
    Herobs
    Herobs
    Sep 19, 2016
    3
    - 取消贪婪匹配。
    - 替换 &nbsp; 不要用正则表达式。
    - 直接用现成的 strip_tags 。
    gouchaoer
    gouchaoer
    Sep 20, 2016 via Android
    4
    用 dom 库做这个啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1367 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 21:24 · PVG 05:24 · LAX 13:24 · JFK 16:24
    ♥ Do have faith in what you're doing.