I've been struggling with a mystery crash most of today which I have finally resolved but don't really understand my own fix.
The purpose of the code is to replace placeholders in a template.
Below is the final minimum PHP code I need to reproduce the problem.
$path = realpath("path_to_template.html");
$content = file_get_contents($path);
$loop_regex = '/{{\*(\w+)}}((?:.|\n)+?){{\/\w+}}/s';
$data = array(
"Trainees"=> array(
array(
"display_name" => "Joe",
"status" => "Pending",
"invited_date" => "01 Sep 2018",
"percentage" => "80%"
)
)
);
preg_match_all($loop_regex, $content, $output_array);
Template:
<table>
<tbody>
<tr>
<th>Trainee</th>
<th>Status</th>
<th>Invited</th>
<th>Score</th>
<th>Action</th>
</tr>
{{*Trainees}}
<tr>
<td>{{display_name}}</td>
<td>{{status}}</td>
<td>{{invited_date}}</td>
<td>{{percentage}}</td>
<td>Some action button</td>
</tr>
{{/Trainees}}
</tbody>
</table>
All was fine until I tried to add more content into the template. All of a sudden, ERR_CONNECTION_RESET whenever it hits the preg_match_all.
The breaking point seems to be related to the size of the content within the {{Trainees}} group only, when it reaches about 395 characters, it breaks.
I found via a Drupal blog that adding this to the Apache httpd.config fixes it.
<IfModule mpm_winnt_module>
ThreadStackSize 8388608
</IfModule>
But I don't really understand why this code would exceed the stack size and therefore potentially I could easily still break it with a bit more content.
Any theories would be welcome.
Environment: WAMPServer 3.0.8, PHP 5.6.25, Apache 2.4.23