PHPiSean Posted December 29, 2011 Share Posted December 29, 2011 Hello everyone, I am an absolute novice when it comes to regex. I could learn it it strongly, but I need to spend much more time than I have. I am stuck with a problem where regex becomes a requirement. In short, I need to use regex to change code submitted by users to divs and htmlentities the code. I commented on the code because explaining there would make much more sense than explaining here. Anyways, it's much like this forum with the code tags. In my code, I've changed the code tags to have pound signs around them just so SMF won't think I'm posting more code inside a code tag. I'm sure there's an escape method, but here's the code: <?php //code outputted from database, I need to regex the code so that #code# is replaced by divs and the code is passed through htmlentities() $stage1 = "This is the free version, here you are #code#<?php echo \"This is the code\"; echo \"<br/>\"; echo \"and some more code\"; ?>#/code#"; //regex //how it should look after the regex process $stage2 = "This is a free version, here you are <div class=code>". htmlentities($regexed_code) . "</div>"; //get rid of slashes added by the mysql_real_escape_string() $stage3 = stripslashes($stage2); echo "<pre>"; echo($stage2); echo "</pre>"; ?> Thanks a lot! Sean Quote Link to comment Share on other sites More sharing options...
ragax Posted December 29, 2011 Share Posted December 29, 2011 Hi Sean, I'm not sure I understand all the details of your request, but to get you started, I've made you a pattern that captures the code. Can you run the code below, confirm that this is what you want to capture, and explain where you want to go from there? <?php $text='<code><?php echo \"This is the code\"; echo \"<br/>\"; echo \"and some more code\"; ?></code>;'; $pattern='%(?s)<code>((??!</code>).)*)</code>%'; preg_match($pattern,$text,$result); echo 'capture: '.htmlentities($result[1]).'<br />'; ?> In the next stage, we'll probably want to use a preg_replace. Wishing you a fun day. Quote Link to comment Share on other sites More sharing options...
sandeep529 Posted December 29, 2011 Share Posted December 29, 2011 Hi, I believe you want to replace the content inside the #code# tags to be replaced by its htmlentitied version. If this is so please try this $stage1 = "This is the free version, here you are #code#<?php echo \"This is the code\"; echo \"<br/>\"; echo \"and some more code\"; ?>#/code#"; function callback($src) { return htmlentities($src[1]); } $stage1= preg_replace_callback("/#code#(.*)#\/code#/sm",'callback',$stage1); echo $stage1; Quote Link to comment Share on other sites More sharing options...
PHPiSean Posted December 29, 2011 Author Share Posted December 29, 2011 Thanks for the reply from both of you. Sandeep, yours has the more desired result with the pound signs (sorry playful I edited my post right when after you viewed it). However, Sandeep's outputs nothing as for now. Moving on, I want to replace the area where the code tags were with <div class=stylearoundcode> and </div>. From the div name, I just want to give them a nice box around it so that it can be distinguished from the rest of the user's post. Also, is there a way I could use brackets such as [ and ] instead of greater than and less than signs? Much like this site does? Quote Link to comment Share on other sites More sharing options...
sandeep529 Posted December 29, 2011 Share Posted December 29, 2011 However, Sandeep's outputs nothing as for now. That seems strange, I have tested this and it was working. Can you tell how you tested? I want to replace the area where the code tags were with <div class=stylearoundcode> and </div> This could be done by change the line in callback function from return htmlentities($src[1]); to return "<div class='stylearoundcode'>".htmlentities($src[1]) ."</div>" ; Quote Link to comment Share on other sites More sharing options...
PHPiSean Posted December 29, 2011 Author Share Posted December 29, 2011 Actually it works brilliantly now! Thanks a ton! By the way, is there a way I could pack this all into a method? I would do it right-off-the-bat but I don't know about the idea of a function inside the function (the callback) The current code I have follows: <?php $stage1 = "This is the free version, here you are #code#<?php echo \"This is the code\"; echo \"<br/>\"; echo \"and some more code\"; ?>#/code# And yeah lol"; function callback($src) { return "<div class='stylearoundcode'><br/>".htmlentities($src[1]) ."</div><br/>" ; } $stage1= preg_replace_callback("/#code#(.*)#\/code#/sm",'callback',$stage1); $stage1 = stripcslashes($stage1); echo "<pre>"; echo $stage1; echo "</pre>"; ?> Thanks again Sandeep! Quote Link to comment Share on other sites More sharing options...
ragax Posted December 29, 2011 Share Posted December 29, 2011 Wow, I went out to buy chocolate, and when I came back the war was over!!! Glad Sandeep was able to sort it out for you, Sean. On objects I definitely cannot help. Wishing you a beautiful end of the year. Quote Link to comment Share on other sites More sharing options...
PHPiSean Posted December 29, 2011 Author Share Posted December 29, 2011 Wow, I went out to buy chocolate, and when I came back the war was over!!! Glad Sandeep was able to sort it out for you, Sean. On objects I definitely cannot help. Wishing you a beautiful end of the year. Thanks again for your help, I read the tutorial in your signature, but go confused right when they dove it. Oh well one of these days I'll take a step-by-step method towards regex. Quote Link to comment Share on other sites More sharing options...
sandeep529 Posted December 29, 2011 Share Posted December 29, 2011 By the way, is there a way I could pack this all into a method? You could create an anonymous function using create_functon so after using that and packing it into a function that your code will become $stage1 = "This is the free version, here you are #code#<?php echo \"This is the code\"; echo \"<br/>\"; echo \"and some more code\"; ?>#/code#"; function escape_code($stage1) { $callback = create_function('$src','return "<div class=\'stylearoundcode\'><br/>".htmlentities($src[1]) ."</div><br/>";'); return preg_replace_callback("/#code#(.*)#\/code#/sm",$callback,$stage1); } echo escape_code($stage1); Quote Link to comment Share on other sites More sharing options...
PHPiSean Posted December 29, 2011 Author Share Posted December 29, 2011 Thank you very much Sandeep. Is there a way I could use this for italics URL and bold tags now? Eg: #i# #b# and #url#? Thank you very much for your help by the way Quote Link to comment Share on other sites More sharing options...
ragax Posted December 31, 2011 Share Posted December 31, 2011 Thank you very much Sandeep. Is there a way I could use this for italics URL and bold tags now? Eg: #i# #b# and #url#? Hi Sean, I haven't followed the whole discussion, but it seems to me that you are just wanting to tweak Sandeep's code a little? For instance, in "/#code#(.*)#\/code#/sm" you want to write url instead of code. In your call back, you want to replace this string: "<div class=\'stylearoundcode\'><br/>" with whatever precedes your url, for instance: "<a href="http://" Same for the string after the url. Last, for the middle of the string, .htmlentities($src[1]). You might want the raw text instead? .$src[1]. I don't know. You have all the elements in your hand now, you should be able to tweak Sandeep's code to your heart's content. Happy new year. Quote Link to comment Share on other sites More sharing options...
ragax Posted January 1, 2012 Share Posted January 1, 2012 Edit: You might want the raw text instead? For thoroughness, I should have mentioned that for the places where you are indeed able to use the raw match (i.e., without putting it through something like strtolower or htmlentities as in Sandeep's example), a simple preg_replace will do---you won't need a callback function as there is nothing to process. 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.