The Little Guy Posted May 12, 2011 Share Posted May 12, 2011 I have these two regexp, and the first one doesn't work the way I wan't, because it is also matching the same stuff as the second regexp and replacing that too, which it shouldn't $content = preg_replace("/\[block\.+)\=(.+)\](.+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1<span style=\"float:right;\">Returns: <span style=\"color: #ffffff;\">$2</span></span></div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$3').'</div>'", $content); $content = preg_replace("/\[block\.+)\](.+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1</div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$2').'</div>'", $content); Here are the two formats of each block, and my first regexp matches both and try's to do stuff. [block:Methods] [item:close()]Closes a connection with a file[/item] [item:duplicate()]Duplicates a file[/item] [item:read()]Reads a file[/item] [item:save()]Saves a file[/item] [item:truncate()]Deletes everything in a file[/item] [/block] [block:Parameters=self] [item:$content]The content you want to save to the file[/item] [item:$filename]The name you want to save the file as[/item] [item:$overwrite](optional [Default = true]) Set to true to overwrite an existing file, set to false to create a new file with a number appended to it[/item] [/block] If you need more info let me know Quote Link to comment Share on other sites More sharing options...
.josh Posted May 12, 2011 Share Posted May 12, 2011 I haven't fully looked at it but at first glance, your issue is probably because you are using greedy quantifiers instead of lazy quantifiers. Use .+? instead of .+ Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 13, 2011 Author Share Posted May 13, 2011 doesn't the uppercase "U" modifier fix that problem? Quote Link to comment Share on other sites More sharing options...
btherl Posted May 13, 2011 Share Posted May 13, 2011 Even if the first regexp is lazy it will still match the entire input (both blocks) eventually. It'll just take longer to find the match. If the block you don't want to match never has an "=" in it, then you can replace all your dots with [^=] and that ought to fix it. Or you can restrict the "=" check to within the open block tag itself, and further limit the character set inside there. Eg if those open block tags only have alphanumeric stuff in them, I would be telling the regexp to only accept alphanumeric. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 13, 2011 Author Share Posted May 13, 2011 Did you mean like this: $content = preg_replace("/\[block\.+)\=(.+)\](.+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1<span style=\"float:right;\">Returns: <span style=\"color: #ffffff;\">$2</span></span></div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$3').'</div>'", $content); $content = preg_replace("/\[block\[^=]+)\]([^=]+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1</div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$2').'</div>'", $content); Here is what that does: http://24.179.144.72:3333/docs/System/Files.php Quote Link to comment Share on other sites More sharing options...
btherl Posted May 13, 2011 Share Posted May 13, 2011 I was thinking of like this: $content = preg_replace("/\[block\[[:alpha:]]+)\=([[:alpha:]]+)\](.+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1<span style=\"float:right;\">Returns: <span style=\"color: #ffffff;\">$2</span></span></div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$3').'</div>'", $content); So by specifying exactly what you want to match on either side of the "=", the expression is less likely to match the wrong thing. The second expression would be: $content = preg_replace("/\[block\[[:alpha:]]+)\](.+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1</div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$2').'</div>'", $content); And it won't match anything with an "=" in the opening block tag, because you've told it only to accept alphabetical characters followed by a closing square bracket. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 13, 2011 Author Share Posted May 13, 2011 Whollah! That looks like it is holding up nicely! Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.