Jump to content


Photo

highlighting code


  • Please log in to reply
7 replies to this topic

#1 xRhysx

xRhysx
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 March 2006 - 08:10 AM

i'm trying to make a tutorial page look really neat, by using the highlight_string() function in php. it doesen't seem to work, i've tried it in so many ways. maybe because of the way it's doing things. i'll explain...

the tutorials are in a database. i'm using a bbcode.php to put in bold text, smilies etc.. and for the php tags, as it has problems with echo'ing these (obviously lol). at the end it returns the edited string.

i'm using echo convert_bbcodes( $content ); to show the tutorial, with all the editited text. i've placed highlight_string() in both files the bbcode and tutorials files, i've placed it everywhere. eg:
echo highlight_string(convert_bbcodes( $content ));
echo convert_bbcodes(highlight_string( $content ));

everytime i've tried it, i either get an error, or it shows the html. any help?

#2 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 14 March 2006 - 08:23 AM

Would need to see the code and the error.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#3 xRhysx

xRhysx
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 March 2006 - 03:59 PM

i'll make 2 example files and post the code in a bit...

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 14 March 2006 - 05:52 PM

[!--quoteo(post=354924:date=Mar 14 2006, 10:59 AM:name=xRhysx)--][div class=\'quotetop\']QUOTE(xRhysx @ Mar 14 2006, 10:59 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
i'll make 2 example files and post the code in a bit...
[/quote]

the issue is that you must TELL highlight_string() to return the value. by default, it only returns a TRUE or FALSE, so to run echo on it, you need to specify it to return the new string:
echo highlight_string(convert_bbcodes( $content ), true);

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 xRhysx

xRhysx
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 March 2006 - 11:51 PM

[!--quoteo(post=354966:date=Mar 14 2006, 05:52 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Mar 14 2006, 05:52 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
the issue is that you must TELL highlight_string() to return the value. by default, it only returns a TRUE or FALSE, so to run echo on it, you need to specify it to return the new string:
echo highlight_string(convert_bbcodes( $content ), true);

hope this helps
[/quote]

no, because that will highlight all of the tutorial. the tutorials contain html aswell, and this causes html to show, not formatted. i need it to select only the php parts, and highlight that bit. i'm using bbcode [php [/code] tags for php code. i've done these files anyway.

example: [a href=\"http://www.mfwp.net/test/test.php\" target=\"_blank\"]here[/a]

test.php
<?php
$dbhost = "localhost";
$dbuser = "*****";
$dbpass = "*****";
$dbname = "*****";
$connection = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());
mysql_select_db($dbname, $connection) or die (mysql_error());

$query = "SELECT * FROM tutorials WHERE category = 'test'";
$result = mysql_query($query, $connection) or die (mysql_error());
$data = mysql_fetch_array($result);
$content = nl2br($data['content']);
$title = $data['title'];

include("bbcode.php");

echo "$title<br><br>";

echo convert_bbcodes( $content );

?>

bbcode.php
<?php
$bb_codes = array(
  '[code]' => '<table width="100%" border="1" cellpadding="3" cellspacing="0" bordercolor="#111111" style="border-collapse: collapse" />
  <tr> 
    <td bgcolor="#DFDFFF" /><strong>CODE</strong></td>
  </tr>
  <tr>
    <td>',
  '
' => '</td></tr></table>'
);

function convert_bbcodes( $t )
{
$search = array_keys( $GLOBALS['bb_codes'] );
$t = str_replace( $search, $GLOBALS['bb_codes'], $t );
$t = eregi_replace("\\[php\\]([^\\[]*)\\[/php\\]", "&lt;?php<br>".highlight_string('\\1', true)."<br>?&gt;",$t);
return $t;

}

?>[/code]

database tutorial:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]hey this is the output i get from the highlight_string function. all tutorials are placed into a database like this.

also bbcode tags are used, which might be the problem, it's echo'ing the highlight_string, which i don't think likes being echo'ed. anyway, examples. you'll see how this is done in the code.

[codetag]
echo "my text or tutorial here, in this format.";
[/codetag][/quote]

hopefully, you'll understand the problem i have, and come up with a soloution. thanks.

#6 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 14 March 2006 - 11:58 PM

You don't need the bb code stuff, but you could pull out the php code highlight it and echo all the html before and after it after doing htmlentities to it. I'm sure someone else might have a better idea, but just off the top of my head...

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#7 xRhysx

xRhysx
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 15 March 2006 - 08:46 PM

i use other stuff with the bbcode, not only the php tags. i can't think of any other way to do it either, stripping out only the php to highlight it.

#8 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 16 March 2006 - 02:21 AM

I was just saying that instead of writing all the code as "
~
", "<?php ~ ?>" becomes "&lt;php ~ ?&gt;", but if you are already using the bbcode stuff anyway no harm doing it that way either. The only thing I can think of is

substr($code, strpos($code, "[php]"), strpos($code, "[/php]")+6);

That will work for $code where only occurs once, and you will need to retrieve the html before and after in a similar manner, but if [code=php:0] occurs more than once you would need a loop to run through them and retrieve all the html between them.

Just a thought, let me know if you come up with something better.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users