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
https://forums.phpfreaks.com/topic/58125-code-in-forums/
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
https://forums.phpfreaks.com/topic/58125-code-in-forums/#findComment-288342
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.