origaflux Posted March 12, 2009 Share Posted March 12, 2009 Sorry if I should've posted this elsewhere, but I don't think anyplace is really any more pertinent. Here's the scenario. I have my site with a blog on it. I employ the html comment tags <!-- and --> in my posts to create private text that only I and one good friend of mine can see. Example: post text blah blah. <!-- private text blah --> post text. I use eregi to remove the text between the tags (and the tags as well) from the post so not even people who snoop around in my html source would be able to see the comments. It works out just fine, until I do something like this: post text blah <!-- private text 1 --> post text 2 <!-- more private text --> more post text. When I use eregi to remove the tags and everything between them, it looks like: post text blah more post text. Note the absence of "post text 2" which was between the two private comments. I know why eregi is doing that, but I want to fix it so it only removes the tags and comments themselves and nothing between. my search for the tags is: eregi("<!--.*-->", $post); I can't wrap my head around how to manipulate this to accomplish my ends. Any help is appreciated and by all means please ask for clarification if I explained oddly. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 12, 2009 Share Posted March 12, 2009 Make the regular expression "non greedy" with the question mark eregi("<!--.*?-->", $post); Quote Link to comment Share on other sites More sharing options...
origaflux Posted March 12, 2009 Author Share Posted March 12, 2009 Well, something about what you said made me think of it right. For whoever may have the same problem, my code turned into this: eregi("(<!--([^(<!--)])*)?-->", $post); Thanks for the indirect help mjdamato. Quote Link to comment Share on other sites More sharing options...
origaflux Posted March 12, 2009 Author Share Posted March 12, 2009 Oops. I lied. The previous item did not fix it. However, thanks anew mjdamato, because your method works just fine. The problem came when i was trying to use it with eregi which is POSIX regex, which is not Perl regex, which means it's not compatible with that syntax. Switching to preg_match worked fine: preg_match("/<!--.*?-->/i", $post); So, now, thanks for the DIRECT help, mjdamato. 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.