Note that there are some explanatory texts on larger screens.

plurals
  1. POGrouping data in pairs
    text
    copied!<blockquote> <p><strong>EDIT:</strong> I feel I should clarify, that I DON'T have a parsing problem to the best of my knowledge. I'm already using DOM and xpath and that feels adequate. The problem I'm having is a logic riddle in using the already parsed results within a loop.</p> </blockquote> <p>I have a long string of HTML I'm trying to parse with PHP. So far, I have preg_match's that are isolating what I want, but I would like some help to refine the output. The HTML is poorly formatted as one long string that is not between any tags, but basically contains headings, followed by one or more paragraphs and so on.</p> <p>So my <code>preg_match</code> regular expressions are isolating the headings and paragraphs, and I'm using a for loop to grab less than or equal to 13 results, because the div into which they will be output has limited dimensions.</p> <p>The amount of headings is an unknown variable, as is the amount of paragraphs per heading. What I am trying do to, is make sure the output ALWAYS ends with a paragraph, regardless of how many previous matches there are (sometimes there are 2 headings in a row. This is a data input issue and I'm working on that too, but have much less control over that). I realise the 'less than or equal' to part of the for loop is problematic. I imagine the solution is to isolate heading-paragraph pairs and then count them, I'm just not making any headway in doing so.</p> <p>Would some kind bright spark point me in the right direction? :)</p> <p>An example of the string I have to work with, is below:</p> <pre><code>&lt;h2 class="border_top"&gt;Description:&lt;/h2&gt; &lt;b&gt;Surroundings&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Amenities&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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&amp;#39;s Quay, offers a regular vehicle service to McInroys Point in Gourock for access to the national road network.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Contact&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;To view this property, please contact &lt;b&gt;Someone Somewhere&lt;/b&gt; on &lt;b&gt;01234 467890&lt;/b&gt; or &lt;b&gt;09876 543210&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Porch/Vestibule 1.7m x 1.8m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Lower Hall 2.7m x 3.65m at widest point&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Provides access to double garage and WC, and to the winding staircase to lower landing area. Solid wooden flooring.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Downstairs Cloakroom 2m x 0.9m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Under stairs WC and wash basin with adequate headroom. Cork effect flooring. Extractor fan. Fitted shelving.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Lower Landing 1.5m x 3.8m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Carpeted. Large cupboard with double doors, fitted with shelving and hanging rail.&lt;br /&gt;Door access to the master bedroom and stairs up to the next landing.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Master Bedroom 4.9m x 5.5m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Dressing Room 3.85m x 2.2m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Carpeted with shelving and hanging rails and with plenty of room for additional storage. Access through door to the en-suite bathroom. Extractor fan.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;En-suite 2m x 3.1m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Main Landing 2.4m x 3.4m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Solid wood flooring. High ceiling with Velux roof light. Access to kitchen, utility room and bedroom/study.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bedroom/Study 4.1m x 4.2m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Kitchen 4.6m x 3.5m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Dining Area 4.6m x 4.3m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Sun Room/Family Room/Office 3.1m x 4.3m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Utility Room 1.65m x 3.05m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Upper Landing 2.7m x 2.3m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Carpeted area with fitted cupboard and access to lounge, bedroom and bathroom.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bathroom&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bedroom 2.75m x 3.9m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Lounge 5.5m x 4.2m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Double Garage 6m x 6.5m&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Plenty of room in this double garage, containing double up and over door and modern gas water heating system.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Garden&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt; &lt;br /&gt; &lt;a name="map"&gt;&lt;/a&gt; </code></pre> <p>As you can see, it lies between 2 tags, which I'm using to locate it in the HTML document. I am using DOM <code>loadHTMLfile</code> and then <code>xpath</code> to query the HTML, but I'm then using <code>preg_match</code> and regular expressions to get the headings and paragraphs from the result. Headings from the above would include Surroundings, Amenities, anything with 0.00m x 0.00m In this particular example, the headings are in bold tags, but this isn't always the case.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload