【问题标题】:Laravel - Split HTML by Tags and Save to DatabaseLaravel - 按标签拆分 HTML 并保存到数据库
【发布时间】:2018-03-16 08:59:02
【问题描述】:

我正在尝试拆分一些 HTML 并将其保存到我的数据库中。这是有问题的 HTML:

<p><strong>NSSDCA/COSPAR ID:</strong> 2009-038F</p>
<p>ANDE 2, the Atmospheric Neutral Density Experiment 2, is a pair of microsatellites (Castor and Pollux) launched from Cape Canaveral on STS 127 on 15 July 2009 at 22:03 UT and deployed from the payload bay of the shuttle on 30 July 2009 at 17:22 UT.</p>
<p><strong>Launch Date:</strong> 2009-07-15<br/><strong>Launch Vehicle:</strong> Shuttle<br/><strong>Launch Site:</strong> Cape Canaveral, United States<br/></p>

我使用 DOM 解析器从外部网站获取此 HTML。我想要做的是将此 HTML 拆分为 mySQL 数据库的五列:

  • COSPAR
  • 说明
  • 发布日期
  • 发射车
  • 启动站点

有可能做到这一点吗?我已经设法使用 Guzzle 为 JSON 和 txt 文件做到这一点,但从来没有使用 HTML。

【问题讨论】:

    标签: php html laravel


    【解决方案1】:

    您可以使用Regex

    示例代码:

    $re = '/<strong>.*?COSPAR.*?<\/strong>(.*)<\/p>\s<p>(.*)\s<p><strong>Launch Date:<\/strong>(.*?)<strong>Launch Vehicle:<\/strong>(.*?)<strong>Launch Site:<\/strong>(.*)/';
    
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    
    // Print the entire match result
    var_dump($matches);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      相关资源
      最近更新 更多