Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>About your string regex: you say it is a string if and only if it is preceded by a white space character or a <code>(</code> and it is directly followed by a <code>,</code> or <code>)</code>. Needles to say, that is not correct. You'd miss strings like:</p> <pre><code>$s = "123"; // ends with a ; $s = "ab\"cd"; // contains an escaped double quote $t = 'efg' ; // is surrounded by single quotes </code></pre> <p>to name just three (there are many more, and what about 'here-docs'?).</p> <p>To account fix the cases above, try something like this:</p> <pre><code>$line = 's = "123"; t = "ab\\\\\\"cd"; u = \'efg\' ; v = \'ef\\\'g\' '; echo $line . "\n"; echo preg_replace('/((["\'])(?:\\\\.|(?:(?!\2).|[^\\\\"\'\r\n]))*\2)/', '&lt;span class="string"&gt;$1&lt;/span&gt;', $line); /* output: s = "123"; t = "ab\\\"cd"; u = 'efg' ; v = 'ef\'g' s = &lt;span class="string"&gt;"123"&lt;/span&gt;; t = &lt;span class="string"&gt;"ab\\\"cd"&lt;/span&gt;; u = &lt;span class="string"&gt;'efg'&lt;/span&gt; ; v = &lt;span class="string"&gt;'ef\'g'&lt;/span&gt; */ </code></pre> <p>A short explanation:</p> <pre><code>( # start group 1 (["\']) # match a single- or double quote and store it in group 2 (?: # start non-matching group 1 \\\\. # match a double quote followed by any character (except line breaks) | # OR (?: # start non-matching group 2 (?!\2). # a character other than what is captured in group 2 | # OR [^\\\\"\'\r\n] # any character except a backslash, double quote, single quote or line breaks ) # end non-matching group 2 )* # end non-matching group 1 and match it zero or more times \2 # the quote captured in group 2 ) # end group 1 </code></pre> <p>Then some comments about your second regex: you first try to match zero or more white space characters. This can safely be omitted because if no white spaces exist you'd still have a match. You could use a <code>\b</code> (word boundary) before matching the function name. Also, <code>(?:[a-z]|[0-9]|_)</code> can be replaced by <code>(?:[a-z0-9_])</code>. And this part of your regex: <code>(@?|!?[a-z]+(?:[a-z]|[0-9]|_)*)</code> which is the same as:</p> <pre><code>( @? | !? [a-z]+ (?: [a-z] | [0-9] | _ )* ) </code></pre> <p>only better indented to see what it actually does. If you look closely, you will see that it will match just <code>@?</code>, and since the <code>@</code> is made optional by the <code>?</code>, that part of your regex will match an empty string as well. No what you'd expected, eh? After that, I must confess I stopped looking at that regex any more, better throw it away.</p> <p>Try something like this to match function names:</p> <pre><code>'/\b[a-z_][a-z0-9_]*(?=\s*\()/i' </code></pre> <p>Which means:</p> <pre><code>\b # a word boundary (the space between \w and \W) [a-z_] # a letter or an underscore [a-z0-9_]* # a letter, digit or an underscore, zero or more times (?= # start positive look ahead \s* # zero ore more white space characters \( # an opening parenthesis ) # end positive look ahead </code></pre> <p>This last one is not tested at all, I leave that for you. Also note that I know very little PHP, so I may be over-simplifying it, in which case it would help if you provide a couple of example code snippets you want to match as functions. </p> <p>Furthermore a word of caution, parsing code using regex-es can be tricky, but if you're only using it to perform highlighting of small snippets of code, you should be fine. When the source files get larger, you might see a drop in performance and you should make some parts of your regex-es <a href="http://www.regular-expressions.info/possessive.html" rel="nofollow noreferrer">"possessive"</a> which will increase the runtime of your matching considerately (especially on larger source files).</p> <p>Lastly, you're probably reinventing the wheel. There exist numerous (well tested) code-highlighters you can use. I suspect you already know this, but I thought it would still be worth mentioning.</p> <p>FYI, I've had good experience with this one: <a href="http://shjs.sourceforge.net/doc/documentation.html" rel="nofollow noreferrer">http://shjs.sourceforge.net/doc/documentation.html</a> </p>
    singulars
    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.
    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