编辑:我觉得我应该澄清一下,据我所知,我没有解析问题。我已经在使用 DOM 和 xpath,感觉已经足够了。我遇到的问题是在循环中使用已经解析的结果的逻辑谜题。
我有一长串 HTML 试图用 PHP 解析。到目前为止,我有 preg_match 来隔离我想要的,但我想要一些帮助来完善输出。HTML 的格式很差,它是一个不在任何标签之间的长字符串,但基本上包含标题,然后是一个或多个段落等。
所以我的preg_match
正则表达式隔离了标题和段落,并且我使用 for 循环来获取小于或等于 13 个结果,因为它们将被输出到的 div 具有有限的尺寸。
标题的数量是一个未知变量,每个标题的段落数量也是如此。我正在尝试做的是确保输出始终以段落结尾,无论之前有多少匹配项(有时连续有 2 个标题。这是一个数据输入问题,我也在努力解决这个问题,但对此的控制要少得多)。我意识到部分 for 循环的“小于或等于”是有问题的。我想解决方案是隔离标题-段落对然后计算它们,我只是没有在这样做方面取得任何进展。
某种明亮的火花会指引我正确的方向吗?:)
我必须使用的字符串示例如下:
<h2 class="border_top">Description:</h2>
<b>Surroundings</b><br /><br />Dunoon is the marine gateway to the Loch Lomond and the Trossachs National Park, and has a number of
local amenities including a cinema, two supermarkets, several choice
restaurants and bars, primary and secondary schools, library, Post
Office, community hospital, doctor surgeries and veterinary clinic,
leisure centre with swimming pool and gym.<br />Innellan has its own
primary school, shop and Post Office. There is a busy village hall and
a popular restaurant nearby. There are a number of clubs in the
Innellan area, including golf, bowling, tennis, cricket and
sailing.<br /><br /><b>Amenities</b><br /><br />A regular local bus
service operates in the area and there are bus and coach services to
popular towns across the West of Scotland. Frequent passenger ferry
services operate from Dunoon Pier to Gourock with regular bus and
train connections to Glasgow Central railway station. The train stops
at Paisley Gilmore Street en route, which is convenient for access to
Glasgow Airport. Western Ferries located at Hunter's Quay, offers
a regular vehicle service to McInroys Point in Gourock for access to
the national road network.<br /><br /><b>Contact</b><br /><br />To
view this property, please contact <b>Someone Somewhere</b> on <b>01234
467890</b> or <b>09876 543210</b><br /><br /><b>Porch/Vestibule 1.7m x
1.8m</b><br /><br />Modern PVCu wood effect front door set between glazed side panels. Cupboard with electricity meter and room for coats
and boots. Solid wooden flooring and shelving. Glazed door to lower
hall.<br /><br /><b>Lower Hall 2.7m x 3.65m at widest point</b><br
/><br />Provides access to double garage and WC, and to the winding
staircase to lower landing area. Solid wooden flooring.<br /><br
/><b>Downstairs Cloakroom 2m x 0.9m</b><br /><br />Under stairs WC and
wash basin with adequate headroom. Cork effect flooring. Extractor
fan. Fitted shelving.<br /><br /><br /><b>Lower Landing 1.5m x
3.8m</b><br /><br />Carpeted. Large cupboard with double doors, fitted with shelving and hanging rail.<br />Door access to the master bedroom
and stairs up to the next landing.<br /><br /><b>Master Bedroom 4.9m x
5.5m</b><br /><br />This large carpeted area featuring 2 large length windows, offering fantastic views of the Firth of Clyde and hills on
the opposite shore. There is also a side window making the room
bright. Access from here through door to the walk-in dressing room
area.<br /><br /><b>Dressing Room 3.85m x 2.2m</b><br /><br />Carpeted
with shelving and hanging rails and with plenty of room for additional
storage. Access through door to the en-suite bathroom. Extractor
fan.<br /><br /><b>En-suite 2m x 3.1m</b><br /><br />Walk-in shower
measuring 1100mm x 800mm, chrome towel rail, bidet, WC and wash basin
inset into fitted cupboards. Privacy glazed window and extractor
fan.<br /><br /><b>Main Landing 2.4m x 3.4m</b><br /><br />Solid wood
flooring. High ceiling with Velux roof light. Access to kitchen,
utility room and bedroom/study.<br /><br /><b>Bedroom/Study 4.1m x
4.2m</b><br /><br />This room which is currently used as a study, would be a generous double bedroom. Large fitted wardrobe. Loft
access. Twin windows with amazing views over the river. Solid wooden
flooring.<br /><br /><b>Kitchen 4.6m x 3.5m</b><br /><br
/>Double-doors from the main landing, leads into this bright and
spacious modern fitted kitchen with lots of storage and countertop,
some providing serving access to the dining area. Double window, tiled
splash-backs and hob extractor hood. Solid wooden flooring. Access to
dining area and to Sun Room/Family Room/Office.<br /><br /><b>Dining
Area 4.6m x 4.3m</b><br /><br />Single window in addition to glazed
French doors that open to a small guarded balcony with wonderful sea
views. Solid wooden flooring continues in from the kitchen to this
bright dining area.<br /><br /><b>Sun Room/Family Room/Office 3.1m x
4.3m</b><br /><br />Currently used as an office, this room has a window to the side and two to the back of the house, together with
patio doors which provide access to the decking. This bright and sunny
room has solid wooden flooring.<br /><br /><b>Utility Room 1.65m x
3.05m</b><br /><br />Worktop with stainless steel sink and tiled splash-back. Fitted shelving. Room for washing machine. Modern PVCu
back door with access to decking and garden. Window to garden.<br
/><br /><b>Upper Landing 2.7m x 2.3m</b><br /><br />Carpeted area with
fitted cupboard and access to lounge, bedroom and bathroom.<br /><br
/><b>Bathroom</b><br /><br />Fitted bath with mixer taps. WC and wash
basin inset into fitted cupboard. Shower unit with electric shower.
Cork effect flooring. Privacy glazed double windows and extractor
fan.<br /><br /><b>Bedroom 2.75m x 3.9m</b><br /><br />Peaceful
generous double bedroom with double window looking out onto the back
garden. Large, fitted, double-door wardrobe with fitted shelving and
hanging rail. Carpeted.<br /><br /><b>Lounge 5.5m x 4.2m</b><br /><br
/>Double-door entrance into this bright carpeted area. The lounge
features a total of 6 windows, 2 single side facing at each end, 2
facing the river as well as 2 x length windows offering fabulous
views of the Firth of Clyde and of the hills on the other side.
Elegant fire surround with living flame gas fire.<br /><br /><b>Double
Garage 6m x 6.5m</b><br /><br />Plenty of room in this double garage,
containing double up and over door and modern gas water heating
system.<br /><br /><b>Garden</b><br /><br />Terraced front garden with
mature planting and additional car parking. Side gate and pathway from
the front of the property leads along the side of the house and up
wooden stairs to timber decking. Decking extends along the rear and
steps up to a small grassed area with flower bed and planters. Pathway
at the other side of the house leads to a planted area at rear of the
house.<br /> <br />
<a name="map"></a>
如您所见,它位于 2 个标签之间,我用它们在 HTML 文档中定位它。我使用 DOM loadHTMLfile
,然后xpath
查询 HTML,但我使用preg_match
正则表达式从结果中获取标题和段落。上面的标题将包括环境、设施、任何 0.00mx 0.00m 在这个特定示例中,标题是粗体标签,但情况并非总是如此。