Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a recursive version. It matches all the quotes in the string. This is how I would do it. If the whole purpose is to display each code better in html, then instead of matching these I would modify the contents with a proper Regex pattern instead.</p> <pre><code>$str = '[quote=&amp;quot;John Doe&amp;quot;:2sxn61wz]' . '[quote=&amp;quot;Bob&amp;quot;:2sxn61wz]' . 'Some text from Bob[/quote:2sxn61wz]' . 'Some text from John Doe[/quote:2sxn61wz]' . 'Some more text'; $str .= '[quote=&amp;quot;MrUpsidown&amp;quot;:2sxn61wz]Some other quote[/quote:2sxn61wz]'; //$str .= '[quote=&amp;quot;MrUpsidown&amp;quot;:2sxn61wz]Yet another one[/quote:2sxn61wz]'; $pat = '~ \[quote=&amp;quot;((?:(?!&amp;quot;).)+)&amp;quot;:(\w+)(?:\]) ( (?(?=\[quote=&amp;quot;(?:(?!&amp;quot;).+)&amp;quot;:\w+\]) (?R) | (?(?!\[/quote:\2\]).) )* ) \[/quote:\2\] ~xis'; preg_match_all($pat,$str,$matches); echo '&lt;h2&gt;Matches:&lt;/h2&gt;'; print_r($matches); $quotes = array(); function replaceQuote($m) { global $curArr; global $pat; $curArr[] = array( 'name' =&gt; $m[1], 'quote' =&gt; preg_replace_callback( $pat,'replaceQuote',$m[3] ) ); return preg_replace($pat,'',$m[0]); } if (!empty($matches[0])) { foreach ($matches[0] as $k =&gt; $v) { $quotes[] = array( array( 'name' =&gt; $matches[1][$k], 'quote' =&gt; $matches[3][$k] ) ); $curArr = &amp;$quotes[count($quotes)-1]; $curArr[0]['quote'] = preg_replace_callback( $pat,'replaceQuote',$curArr[0]['quote'] ); } unset($curArr); } echo '&lt;h2&gt;Result:&lt;/h2&gt;'; print_r($quotes); </code></pre> <p>And here's the output.</p> <blockquote> <h2>Matches:</h2> <pre><code> Array ( [0] =&gt; Array ( [0] =&gt; [quote="John Doe":2sxn61wz][quote="Bob":2sxn61wz]Some text from Bob[/quote:2sxn61wz]Some text from John Doe[/quote:2sxn61wz] [1] =&gt; [quote="MrUpsidown":2sxn61wz]Some other quote[/quote:2sxn61wz] ) [1] =&gt; Array ( [0] =&gt; John Doe [1] =&gt; MrUpsidown ) [2] =&gt; Array ( [0] =&gt; 2sxn61wz [1] =&gt; 2sxn61wz ) [3] =&gt; Array ( [0] =&gt; [quote="Bob":2sxn61wz]Some text from Bob[/quote:2sxn61wz]Some text from John Doe [1] =&gt; Some other quote ) ) </code></pre> <h2>Result:</h2> <pre><code> Array ( [0] =&gt; Array ( [0] =&gt; Array ( [name] =&gt; John Doe [quote] =&gt; Some text from John Doe ) [1] =&gt; Array ( [name] =&gt; Bob [quote] =&gt; Some text from Bob ) ) [1] =&gt; Array ( [0] =&gt; Array ( [name] =&gt; MrUpsidown [quote] =&gt; Some other quote ) ) ) </code></pre> </blockquote>
 

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