Rob Rob - 2 months ago 7
PHP Question

Stream HTTP response of parsed template including loops

I have a database containing millions of items. These items are to be exportable via web interface. The customer can specify a template (string) with placeholders on how to export the data.

Since the datasets are vey large, I cannot do a simple search/replace on the template and send the result as a response, since this would eat away my memory. So I stream the response via Transfer-Encoding: chunked.

A template may look like this:

Some static content at the beginning …

[foreach item]{item.id} {item.name}[/foreach]

Some static content in between the loops …

[foreach item]{item.id} {item.value}[/foreach]

Some static content at the end …


How can I stream this sequentially? If there was only one foreach loop, I could simply divide the streaming process in three stages:


  1. Stream content before loop line by line.

  2. Iterate through loop and stream each item line by line.

  3. Stream content after loop line by line.



However, the template may contain 0..n foreach loops (based on the same item list). Is there a regex to extract all loops and the contents in between, so I can iterate over them?

Rob Rob
Answer

I solved it, by utilizing preg_split() to cut the template in stream-able pieces by defining foreach blocks as separator:

$segments = preg_split('@(\[foreach .+?\].*?\[/foreach\])@si', $template, NULL, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);

It’s important to set PREG_SPLIT_DELIM_CAPTURE in order to capture the foreach blocks, too.