热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

基于分隔符切片HTML

如何解决《基于分隔符切片HTML》经验,为你挑选了1个好方法。



1> Hugo Delsing..:

为了解决这样的问题,您首先需要在开始编码之前确定获得解决方案所需的步骤.

    找到以[[delimiter]]开头的元素

    检查它的父母是否有 next sibling

    没有?重复2

    是?下一个兄弟包含内容.

现在,一旦你开始使用它,你已经准备好了90%.您需要做的就是清理不必要的标签,然后就完成了.

为了获得可以扩展的东西,不要构建一个可以工作的混淆代码的市长堆,而是将您需要的所有数据拆分成可以使用的东西.

下面的代码可以使用两个完全符合您需求的类,并在您需要时为您提供一个很好的方法来遍历所有元素.它确实使用PHP Simple HTML DOM Parser而不是DOMDocument,因为我更喜欢它.


                

[[delimiter]]Start of content section 1.

More content in section 1

[[delimiter]]Start of section 2

More content in section 2

[[delimiter]]Start of section 3

More content in section 3
XML; /* * CALL */ $parser = new HtmlParser($html, '[[delimiter]]'); //dump found //decode/encode to only show public values print_r(json_decode(json_encode($parser))); /* * ACTUAL CODE */ class HtmlParser { private $_html; private $_delimiter; private $_dom; public $Elements = array(); final public function __construct($html, $delimiter) { $this->_html = $html; $this->_delimiter = $delimiter; $this->_dom = str_get_html($this->_html); $this->getElements(); } final private function getElements() { //this will find all elements, including parent elements //it will also select the actual text as an element, without surrounding tags $elements = $this->_dom->find("[contains(text(),'".$this->_delimiter."')]"); //find the actual elements that start with the delimiter foreach($elements as $element) { //we want the element without tags, so we search for outertext if (strpos($element->outertext, $this->_delimiter)===0) { $this->Elements[] = new DelimiterTag($element); } } } } class DelimiterTag { private $_element; public $Content; public $MoreContent; final public function __construct($element) { $this->_element = $element; $this->COntent= $element->outertext; $this->findMore(); } final private function findMore() { //we need to traverse up until we find a parent that has a next sibling //we need to keep track of the child, to cleanup the last parent $child = $this->_element; $parent = $child->parent(); $next = null; while($parent) { $next = $parent->next_sibling(); if ($next) { break; } $child = $parent; $parent = $child->parent(); } if (!$next) { //no more content return; } //create empty element, to build the new data //go up one more element and clean the innertext $more = $parent->parent(); $more->innertext = ""; //add the parent, because this is where the actual content lies //but we only want to add the child to the parent, in case there are more delimiters $parent->innertext = $child->outertext; $more->innertext .= $parent->outertext; //add the next sibling, because this is where more content lies $more->innertext .= $next->outertext; //set the variables if ($more->tag=="body") { //Your section 3 works slightly different as it doesn't show the parent tag, where the first two do. //That's why i show the innertext for the root tag and the outer text for others. $this->MoreCOntent= $more->innertext; } else { $this->MoreCOntent= $more->outertext; } } } ?>

清理输出:

stdClass Object
(
  [Elements] => Array
  (
    [0] => stdClass Object
    (
        [Content] => [[delimiter]]Start of content section 1.
        [MoreContent] => 

[[delimiter]]Start of content section 1.

More content in section 1

) [1] => stdClass Object ( [Content] => [[delimiter]]Start of section 2 [MoreContent] =>

[[delimiter]]Start of section 2

More content in section 2
) [2] => stdClass Object ( [Content] => [[delimiter]]Start of section 3 [MoreContent] =>

[[delimiter]]Start of section 3

More content in section 3
) ) )


我已经决定这是一个有趣的理论练习,但却是一个实际的噩梦.您可能需要数百个示例来确保任何解决方案都能正常工作,然后正如您所说,有人带着示例101来再次破坏代码.
TBH - 没有OP的输入,甚至很难验证对该文档所做的基本假设.即使是"父母有下一个兄弟"逻辑的跟踪备份文档的基础也可能过于简单化了可能的组合.
推荐阅读
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 本文介绍了一种在PHP中对二维数组根据某个字段进行排序的方法,以年龄字段为例,按照倒序的方式进行排序,并给出了具体的代码实现。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
author-avatar
呆子只爱小呆
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有