brady123 Posted November 12, 2011 Share Posted November 12, 2011 I'm creating a custom BBCode for a code area, much like this site has. My problem is, if I use it twice on one page, the regex will take the opening tag of the first usage, and the closing tag of the second usage. I've played with it forever and can't figure it out. Any ideas on how I can make it stick with the first open/first close, etc? Some type of for each loop? I'm completely stuck and lost. Thanks! Here's my code I'm using... $str = preg_replace("/\[code\](.*?)\[\/code\]/esiU", "'<span style=\"font-size:9px;\">Code:</span><br /><div class=\"code\">'.stripslashes(htmlspecialchars('\\1')).'<br /></div>'", $str); Quote Link to comment https://forums.phpfreaks.com/topic/251009-using-preg_replace-twice-not-working/ Share on other sites More sharing options...
silkfire Posted November 12, 2011 Share Posted November 12, 2011 Why do you need to use it twice? Quote Link to comment https://forums.phpfreaks.com/topic/251009-using-preg_replace-twice-not-working/#findComment-1287647 Share on other sites More sharing options...
brady123 Posted November 12, 2011 Author Share Posted November 12, 2011 For something like this: <?php echo "here's some code"; ?> Now I will explain more about this next section... <?php // next part ?> My code is wrapping it all in one <code> section, using the open tag from the first one, and the close tag from the second. Quote Link to comment https://forums.phpfreaks.com/topic/251009-using-preg_replace-twice-not-working/#findComment-1287652 Share on other sites More sharing options...
silkfire Posted November 12, 2011 Share Posted November 12, 2011 Your explanations is very unclear.... show us some example input and result, that's easier to understand. Quote Link to comment https://forums.phpfreaks.com/topic/251009-using-preg_replace-twice-not-working/#findComment-1287676 Share on other sites More sharing options...
brady123 Posted November 13, 2011 Author Share Posted November 13, 2011 Sorry, here is a more clear explanation of what I am trying to do. Because I'm trying to use code inside code, It messes it up, so I'll have to leave a space between. Say I have the following code BEFORE parsing: [ code] Here is some code [ /code] Here is some text outside of the first code block. [ code] Here is a second block of code. [ /code] And the output that I'm getting is: <code> Here is some code [ /code] Here is some text outside of the first code block. [ code] Here is a second block of code. </code> Does that make sense? It's parsing the opening tag from the first code section, and the closing tag of the second code section. See how I am able to use the CODE section twice in this current post? I can't get that to work on my site. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/251009-using-preg_replace-twice-not-working/#findComment-1287751 Share on other sites More sharing options...
silkfire Posted November 13, 2011 Share Posted November 13, 2011 I ran your regex and got this: <span style="font-size:9px;">Code:</span><br /><div class="code"> Here is some code <br /></div> Here is some text outside of the first code block. <span style="font-size:9px;">Code:</span><br /><div class="code"> Here is a second block of code. <br /></div> Seems okay to me? Quote Link to comment https://forums.phpfreaks.com/topic/251009-using-preg_replace-twice-not-working/#findComment-1287772 Share on other sites More sharing options...
silkfire Posted November 13, 2011 Share Posted November 13, 2011 Forgot to post the PHP code: $str = preg_replace('#\[ *?code *?\](.*?)\[ *?/code *?\]#sie', '<span style="font-size:9px;">Code:</span><br /><div class="code">' . stripslashes(htmlspecialchars('\\1')) . '<br /></div>', $str); I think what messes up for you is the U flag; why you use it here I don't know. It inverts greediness, something you do not need in this case. Quote Link to comment https://forums.phpfreaks.com/topic/251009-using-preg_replace-twice-not-working/#findComment-1287773 Share on other sites More sharing options...
brady123 Posted November 13, 2011 Author Share Posted November 13, 2011 Forgot to post the PHP code: $str = preg_replace('#\[ *?code *?\](.*?)\[ *?/code *?\]#sie', '<span style="font-size:9px;">Code:</span><br /><div class="code">' . stripslashes(htmlspecialchars('\\1')) . '<br /></div>', $str); I think what messes up for you is the U flag; why you use it here I don't know. It inverts greediness, something you do not need in this case. Ah, something so simple, removing that darn flag. Thank you VERY much! You're a genius!! Quote Link to comment https://forums.phpfreaks.com/topic/251009-using-preg_replace-twice-not-working/#findComment-1287849 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.