Jump to content

[code] In forums


Grego

Recommended Posts

In the forums I'm making, I'm using BBcode. I have a table containing translations. For example, "" is translated to "<b>". This way, I can limit what my users can do to manipulate the page, obviously.

 

Anyhow, I want to include [ code] tags, but I can't figure out a way to stop text inside [ code] tags from being converted through the table.

 

Tell me if I haven't explained that properly/enough.

Link to comment
Share on other sites

You can "protect" certain code by removing it while performing other procedures.  If you're already used htmlentities() to convert such characters as <> to < and >, you can replace [ code ] ...[ /code ] with <1>, <2>, etc, perform the other translations, and restore the original code sections by replacing their placeholders.

 

I've used something like this:

<?php

function save_code(&$index_ref,&$array_ref,$code) {
$array_ref[$index_ref] = str_replace('\"','"',$code);
return '<' . $i_ref++ . '>';
}

$source = htmlentities($source);
$saved_code = array();
$sc_index = 0;

preg_replace(array(
'|\[code\](.*?)\[/code\]|ie',  // Remove and store stuff here
'/other replacements/',
'/<(\d+)>/e' // Restore it here
),array(
'save_code($sc_index,$saved_code,$1)',
'....',
'$saved_code[$1]'
),$source);

?>

Link to comment
Share on other sites

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.