douhan8581 2017-10-04 20:39
浏览 62
已采纳

Laravel - 按标签拆分HTML并保存到数据库

I am trying to split some HTML and save it to my database. This is the HTML in question:

<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>

I get this HTML from a external website using a DOM Parser. What I want to do is to split this HTML in five columns for the mySQL database:

  • COSPAR
  • Description
  • Launch Date
  • Launch Vehicle
  • Launch Site

Would it be possible to do this? I have managed to do this with Guzzle for JSON and txt files but never HTML.

</div>
  • 写回答

1条回答 默认 最新

  • drodsh7940 2017-10-04 21:53
    关注

    You can use Regex

    Example code:

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率