1

I need to extract from this html:

<div class="list-group">
    <a class="list-group-item active">
        <h4 class="list-group-item-heading">{EVENT_GROUP_TIME}</h4>
    </a>
    {EVENTS:}
    <a class="list-group-item">
        <h4 class="list-group-item-heading">{EVENT_NAME}</h4>
        <p class="list-group-item-text"><i>{EVENT_LECTURER}</i></p>
        <p class="list-group-item-text">{EVENT_TIME}</p>
        <p class="list-group-item-text">{EVENT_LOCATION}</p>
    </a>
    {ENDEVENTS}
</div>

Two substring: with {EVENTS:}...{ENDEVENTS} block and second is everything else. In other words: $group_header =

<div class="list-group">
    <a class="list-group-item active">
        <h4 class="list-group-item-heading">{EVENT_GROUP_TIME}</h4>
    </a>
</div>

and $group_body =

<a class="list-group-item">
    <h4 class="list-group-item-heading">{EVENT_NAME}</h4>
    <p class="list-group-item-text"><i>{EVENT_LECTURER}</i></p>
    <p class="list-group-item-text">{EVENT_TIME}</p>
    <p class="list-group-item-text">{EVENT_LOCATION}</p>
</a>

I tried to make it with substr but it seems to be too slow with big date. Can someone suggest how to make such operation with regexp in php?

ovnia
  • 2,412
  • 4
  • 33
  • 54
  • Have you tried using something like: http://simplehtmldom.sourceforge.net/ instead of regex? – ghstcode Dec 01 '13 at 10:33
  • No, i dont. The reason is that i need to parse just one "template" and using the whole lib is not what i want. But thing is cool. Added to bookmarks =) – ovnia Dec 01 '13 at 10:37
  • As i said, i tried to do it with substr, strlen and strpos. Also tried some regexp matches like {EVENTS:}(.*?){ENDEVENTS} but it dont work @Nimrod007 – ovnia Dec 01 '13 at 10:59
  • @user3017651 the expected output doesn't match the input you have, think about the closing `` – HamZa Dec 01 '13 at 11:21
  • You use XML, which has all the functionality for XPath and XQuery based extractions of data. And then you ruin it by stupid curly braces? Don't forget to shot the bonehead who had that idea. – ceving Dec 01 '13 at 12:12
  • This might help: http://stackoverflow.com/questions/13824438/php-preg-replace-everything-in-between-specific-html-comment-tags/13824617#13824617 – Onimusha Dec 01 '13 at 18:40

1 Answers1

1

The Regular Expression You Need is this:

/(.*)({EVENTS:}(.*){ENDEVENTS})(.*)/s

Here is the complete PHP Code to test it:

<?php
$regex = "/(.*)({EVENTS:}(.*){ENDEVENTS})(.*)/s";
$string = "
<div class=\"list-group\">
    <a class=\"list-group-item active\">
        <h4 class=\"list-group-item-heading\">{EVENT_GROUP_TIME}</h4>
    </a>
    {EVENTS:}
    <a class=\"list-group-item\">
        <h4 class=\"list-group-item-heading\">{EVENT_NAME}</h4>
        <p class=\"list-group-item-text\"><i>{EVENT_LECTURER}</i></p>
        <p class=\"list-group-item-text\">{EVENT_TIME}</p>
        <p class=\"list-group-item-text\">{EVENT_LOCATION}</p>
    </a>
    {ENDEVENTS}
</div>

";
preg_match($regex,$string,$matches);
print_r($matches);
?>

Reference for the "s" modifier:

http://php.net/manual/en/reference.pcre.pattern.modifiers.php

Naveed Hasan
  • 641
  • 7
  • 19