Rantzien Posted March 15, 2007 Share Posted March 15, 2007 Well, this is not directly PHP, but I'm writing it in PHP, and it's also the language I'm currently trying to style. Anyway, I am trying to make a function to highlight code syntax properly (for example, giving strings one style, keywords another style, operators yet another style). Now, I hit an obstacle that I have trouble finding my way around.Consider this code:"foo ${"sometext"} bar" . "a little more text"The desired result is this:"foo ${"sometext"} bar" . "a little more text"That is, I want the string regex to match: "foo ${"sometext"} bar", "sometext" and "a little more text", which I then wrap with <span> tags to get the desired result.The variable within the string is already formatted, with this regex (pattern -> replacement):"/[$]{1,2}\{.*\}/sU" -> "<span class=\"default\">$0</span>"What I am confused about is this: I don't want the string regex to be a simple greedy one, like this: "/\".*\"/s" (since it would match everything from the first to the last double quote). But I don't want it to be a simple ungreedy one either, like this: "/\".*?\"/s" (since that would match "foo ${", "} bar" and "a little more text").I've tried doing: /".*(\{)?.*\1.*"/sU, but that makes the function fail. If you can help me, I'd be very grateful. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 15, 2007 Share Posted March 15, 2007 So is this just a regexp question? Cause I hate to burst your bubble, but PHP already has a built-in PHP syntax highlighter:http://www.php.net/manual/en/function.highlight-string.phpBut, if you're just doing this for the learning value, then.. well, someone who knows more about regular expressions I'm sure will be right along to help you. If I was doing this, I probably wouldn't use regexp, I would probably just use some string parsing functions, maybe strtok, and keep track of things like opened and closed quotes, escaped quotes, etc myself. Link to comment Share on other sites More sharing options...
Rantzien Posted March 16, 2007 Author Share Posted March 16, 2007 So is this just a regexp question? Cause I hate to burst your bubble, but PHP already has a built-in PHP syntax highlighter:http://www.php.net/manual/en/function.highlight-string.php Oh my, I didn't know of that function. Thanks But, if you're just doing this for the learning value, then.. well, someone who knows more about regular expressions I'm sure will be right along to help you. If I was doing this, I probably wouldn't use regexp, I would probably just use some string parsing functions, maybe strtok, and keep track of things like opened and closed quotes, escaped quotes, etc myself.Well, I was partly doing this for training, but also to have it ready in case I needed it. And yeah, I recently scrapped the idea of using regular expressions in favour of parsing the string, although it's complex.There aren't any similar built-in functions for parsing other languages, are there?Thanks for your help! Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2007 Share Posted March 16, 2007 No, you can either highlight a string of code or an entire file with highlight_string and highlight_file, but it only highlights PHP. You may be able to find PHP highlighters for other languages though if you bust a Goog. Link to comment Share on other sites More sharing options...
Rantzien Posted March 16, 2007 Author Share Posted March 16, 2007 No, you can either highlight a string of code or an entire file with highlight_string and highlight_file, but it only highlights PHP. You may be able to find PHP highlighters for other languages though if you bust a Goog.I figure I could, but if it's not built in I might as well do it myself Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now