Cetanu Posted December 14, 2009 Share Posted December 14, 2009 I have a simple (?) preg_replace statement that I would like to replace a { with a <, but I keep getting it wrong and right now RegEx really confuses me, so help is appreciated. preg_replace("/(\{.*)/", "<", $message); Thanks in advance. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted December 14, 2009 Share Posted December 14, 2009 Understand that the star quantifier (*) is greedy.. so odds are, its capturing more than you want.. perhaps without seeing an example, the best guess I throw at the moment would be to make it lazy, so .* becomes .*? (but I suspect there might be more to it - perhaps showing a sample string?) [ot] I'm not sure you need to use a capture either (you can eliminate the parenthesis). Also, you don't need to esacpe the { character in your pattern. [/ot] Quote Link to comment Share on other sites More sharing options...
Cetanu Posted December 14, 2009 Author Share Posted December 14, 2009 This is a function similar to BBCode. I would like something like "{u}Hello{/u}" to be changed into <u>Hello</u>. The code snippet I showed is just for left bracket, but I assumed I could go from there after learning what I'm doing wrong. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 14, 2009 Share Posted December 14, 2009 the following should work try it out <?php // check fields $str = '{u}Hello{/u}'; echo preg_replace('#\{{1,1}(.*?)\}{1,1}#',"<$1>", $str); ?> Quote Link to comment Share on other sites More sharing options...
Cetanu Posted December 14, 2009 Author Share Posted December 14, 2009 I've checked that code outside of my file, and it worked. Inside of my file, it does not and I don't know why. if(!empty($_POST['submit'])){ connect($db="government"); preg_replace('#\{{1,1}(.*?)\}{1,1}#',"<$1>", $message); echo $message; echo "<script>alert('Success!'); location='editCont.php?page=editNews';</script>"; } Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 14, 2009 Share Posted December 14, 2009 your code should be, preg_replace returns the replaced string. $message = preg_replace('#\{{1,1}(.*?)\}{1,1}#',"<$1>", $message); Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted December 14, 2009 Share Posted December 14, 2009 @Cetanu Do a forum search on the topic of regex and bbc, as this is a re-occuring theme... I don't know the threads off hand, but this has been asked (and answered) plenty of times before. @rajivgonslaves declaring intervals like {1,1} is not terribly useful, as it is the same thing as the character(s) that precedes it (only I would wager this would be slower). So doing something like 'a{1,1}' is the same thing as simply stating 'a'. Also note that you don't need to escape the curly braces (it's a common misconception - there might be some circumstances where it might be needed, but I'm finding that in general, it isn't).. your pattern could be more along the lines of: #{(.*?)}# [ot] Granted, either way, patterns like this could theoritically match '{I am a string inside curly braces} and alter that.. depending on the OP's circumstances, this may not be desirable. Again, there are threads within this forum that discuss bbc tag parsing. [/ot] 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.