I need to replace <slot> slot_name </slot> with a <?php !include_slot('slot_name')?> in the text that I read from file
<table class="layout-table" id="layout1">
<tr>
<td class="slot" id="slot1" colspan="2">
<slot>
slot_name
</slot>
</td>
</tr>
<tr>
<td class="slot" id="slot2" rowspan="2">
<slot>
slot_name
</slot>
</td>
<td class="slot" id="slot3">
<slot>
slot_name
</slot>
</td>
</tr>
</table>
could anybody give me some directions as I had not really worked with this kind of traversing before. The problem is to iterate through the text and at the same time change the block with respect to the “slot_name”
Since you appear to be doing a straight search-and-replace, and not actually parsing HTML or XML, doing a regex here is a perfectly valid option.
(If you might have existing PHP containing this slot stuff, or otherwise start getting into nested tags, commenting, and similar, you’ll be wanting to use a DOM parser.)
This one uses lookahead/lookbehind to mean that the whole match is slot_name:
Alternatively, this will place the slot_name into capture group 1:
(These both assume that slot_name is comprised of “word characters”, which is alphanumerics and underscore.)
Explanation of the first one is:
The second lacks the lookaheads, but uses simple cature group syntax
(…)but is otherwise no new syntax.(If you do want to learn regular expressions fully, regular-expressions.info has a tutorial worth completing.)
So yeah, either one of these lines will do it:
(Note the escaped forward slashes – alternatively you can use different characters at the start/end to delimit the regex.)