Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>Please note</em>: this is <em>not</em> an attempt to provide a fix for the regex. It is just here to show how difficult it is (dare I say impossible) to create a regex that will successfully parse HTML. Even well structured XHTML would be nightmarishly difficult, but poorly structured HTML is a no-go for regular expressions.</p> <p>I agree 100% that using regular expressions to attempt HTML parsing is a very bad idea. The following code uses the supplied function to parse some simple HTML tags. It trips up on its second attempt when it finds the nested HTML tag <code>&lt;em&gt;Test&lt;em&gt;</code>:</p> <pre><code>$t['label'] = 'Test'; $text = '&lt;p&gt;Test&lt;/p&gt;'; $find = '/(?![^&lt;]+&gt;)(?&lt;!\w)(' . preg_quote($t['label']) . ')\b/s'; $text = preg_replace_callback($find, 'replaceCallback', $text); echo "Find: $find\n"; echo 'Quote: ' . preg_quote($t['label']) . "\n"; echo "Result: $text\n"; /* Returns: Find: /(?![^&lt;]+&gt;)(?&lt;!\w)(Test)\b/s Quote: Test Result: &lt;p&gt;&lt;a class="tag" rel="tag-definition" title="Click to know more about Test" href="?tag=Test"&gt;Test&lt;/a&gt;&lt;/p&gt; */ $t['label'] = '&lt;em&gt;Test&lt;/em&gt;'; $text = '&lt;p&gt;Test&lt;/p&gt;'; $find = '/(?![^&lt;]+&gt;)(?&lt;!\w)(' . preg_quote($t['label']) . ')\b/s'; $text = preg_replace_callback($find, 'replaceCallback', $text); echo "Find: $find\n"; echo 'Quote: ' . preg_quote($t['label']) . "\n"; echo "Result: $text\n"; /* Returns: Find: /(?![^&lt;]+&gt;)(?&lt;!\w)(Test)\b/s Quote: Test Result: &lt;p&gt;&lt;a class="tag" rel="tag-definition" title="Click to know more about Test" href="?tag=Test"&gt;Test&lt;/a&gt;&lt;/p&gt; Warning: preg_replace_callback() [function.preg-replace-callback]: Unknown modifier '\' in /test.php on line 25 Find: /(?![^&lt;]+&gt;)(?&lt;!\w)(\&lt;em\&gt;Test\&lt;/em\&gt;)\b/s Quote: \&lt;em\&gt;Test\&lt;/em\&gt; Result: */ function replaceCallback($match) { if (is_array($match)) { $htmlVersion = $match[1]; $urlVersion = urlencode($htmlVersion); return '&lt;a class="tag" rel="tag-definition" title="Click to know more about ' . $htmlVersion . '" href="?tag=' . $urlVersion . '"&gt;' . $htmlVersion . '&lt;/a&gt;'; } return $match; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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