Jump to content

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/251009-using-preg_replace-twice-not-working/
Share on other sites

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.

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.

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?

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.

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!! :D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.