【问题标题】:PHP regular expression to parse HTML in a specific way [duplicate]PHP正则表达式以特定方式解析HTML [重复]
【发布时间】:2012-10-18 16:49:45
【问题描述】:

可能重复:
How to parse and process HTML with PHP?

我有以下 HTML:

<Text><strong>Cancellation</strong>
A cancellation is free of charge possible until 6PM on arrival day. With later    cancellation or no arrivals without previous cancellation the hotel is justified to place 90% of the booked achievements (excl. breakfast) in calculation whereby the amount at a value of USD 500.00 is not exceeded.
<strong>Check-in time</strong>
Starting from 15:00
<strong>Check-out time</strong>
Until 12:00
<strong>Guarantee</strong>
No guarantee required.</Text>

我想解析它并存储在一个数组中,例如:

array(
 'Cancelation'    => 'A cancellation is free of charge possible until 6PM on arrival day. With later    cancellation or no arrivals without previous cancellation the hotel is justified to place 90% of the booked achievements (excl. breakfast) in calculation whereby the amount at a value of USD 500.00 is not exceeded.',
 'Check-in time'  => 'Starting from 15:00',
 'Check-out time' => 'Until 12:00',
 'Guarantee'      => 'No guarantee required.'
);

提前感谢您的提示。

【问题讨论】:

标签: php html regex


【解决方案1】:

阅读this 教程,无需使用饥饿的正则表达式引擎。

【讨论】:

  • 我正在尝试将 XML 加载到 Dom 对象中,但之后我无法解析所有 标签的内容以及它们之间的文本。
  • @user1755641 使您的 HTML/XML 有效。然后,解析所有strong 标签,以及中间的文本...
  • 同意@Hidde,如果你的HTML是动态生成的,那么考虑返工它的生成过程。
【解决方案2】:

有效 HTML 添加到DOMElement 对象中。有了它,您可以选择孩子并将他们的 HTML/文本提取到变量中。

文档:http://php.net/manual/en/class.domelement.php

【讨论】:

  • 这仅适用于有效的 html。这显然不是。
  • 嗯,输入的html还是无效的。所以你的解决方案行不通。
  • 我正在为他按照他应该的方式构建数据的情况提供一个解决方案。然后,这将起作用,并且比 REGEX 解析要好得多。
  • 我是说他需要编辑他的 HTML/XML,无论如何他都应该这样做,然后解决他的问题!
  • 我无法编辑我以这种方式从另一个系统收到的 xml/html。
猜你喜欢
  • 1970-01-01
  • 2011-01-23
  • 2018-04-29
  • 2016-06-15
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多