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?

6
  • Have you tried using something like: simplehtmldom.sourceforge.net instead of regex? Commented Dec 1, 2013 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 =) Commented Dec 1, 2013 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 Commented Dec 1, 2013 at 10:59
  • @user3017651 the expected output doesn't match the input you have, think about the closing </div> Commented Dec 1, 2013 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. Commented Dec 1, 2013 at 12:12

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

How would you match the rest ?
I edited my answer so that other parts are also matched. (Match 1) + (Match 3) is the rest, with the brackets.
Use the s modifier to match newlines with .. You'll end up with something like (.*){EVENTS:}.*?{ENDEVENTS}(.*)
I am checking right now, it doesnt seem to work, wait.. Only the {EVENTS:} thing is getting matched.
How are you printing the results ? Can you show me in a pastebin ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.