Note that there are some explanatory texts on larger screens.

plurals
  1. POAt certain points in the HTML, I need to close every open tag, insert a DIV, then open all tags again, in order
    text
    copied!<p>I am dealing with HTML that's been generated with <a href="http://sourceforge.net/projects/fckeditor/" rel="nofollow">FCKeditor</a>. So it will look something like this:</p> <pre><code>&lt;p&gt;Paragraph&lt;/p&gt; &lt;ul&gt; &lt;li&gt;List item&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>No <code>head</code> tag, no <code>body</code> tag, just a snippet of HTML.</p> <p>I am trying to add support for certain variables that, when inserted into the HTML, will be replaced with dynamic content. So the HTML, variable inserted, might look like this:</p> <pre><code>&lt;p&gt;Here's a variable: {widget}&lt;/p&gt; </code></pre> <p>I want to replace <code>{widget}</code> with this:</p> <pre><code>&lt;div class="widget"&gt;Hi, I'm a widget.&lt;/div&gt; </code></pre> <p>FCKeditor encapsulates content (rightly) into paragraphs when you insert a line break. So if I did a straight replace, the resulting HTML would be this:</p> <pre><code>&lt;p&gt;Here's a variable: &lt;div class="widget"&gt;Hi, I'm a widget.&lt;/div&gt;&lt;/p&gt; </code></pre> <p>That's not going to work because the <code>div</code> tag is inside of the <code>p</code> tag. So what I want to do is close the paragraph and insert the DIV after it:</p> <pre><code>&lt;p&gt;Here's a variable: &lt;/p&gt; &lt;div class="widget"&gt;Hi, I'm a widget.&lt;/div&gt; </code></pre> <p>Let's take this example:</p> <pre><code>&lt;p class="someclass"&gt;Here's a &lt;strong&gt;variable: {widget} more&lt;/strong&gt; content after&lt;/p&gt; </code></pre> <p>I would want this result:</p> <pre><code>&lt;p class="someclass"&gt;Here's a &lt;strong&gt;variable: &lt;/strong&gt;&lt;/p&gt; &lt;div class="widget"&gt;Hi, I'm a widget.&lt;/div&gt; &lt;p class="someclass"&gt;&lt;strong&gt; more&lt;/strong&gt; content after&lt;/p&gt; </code></pre> <p><strong>At every instance of <code>{widget}</code> in HTML snippet, I need to make a "break" in the HTML. Which is to close every open tag, insert the widget code, then open them all again in order.</strong></p> <p>Is this possible using a PHP HTML parser? If so, how would I go about it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload