Jump to content

Recommended Posts

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 [code=php:0][/code] 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?
Link to comment
https://forums.phpfreaks.com/topic/4907-highlighting-code/
Share on other sites

[!--quoteo(post=354924:date=Mar 14 2006, 10:59 AM:name=xRhysx)--][div class=\'quotetop\']QUOTE(xRhysx @ Mar 14 2006, 10:59 AM) [snapback]354924[/snapback][/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:
[code]
echo highlight_string(convert_bbcodes( $content ), true);
[/code]

hope this helps
Link to comment
https://forums.phpfreaks.com/topic/4907-highlighting-code/#findComment-17440
Share on other sites

[!--quoteo(post=354966:date=Mar 14 2006, 05:52 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Mar 14 2006, 05:52 PM) [snapback]354966[/snapback][/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:
[code]
echo highlight_string(convert_bbcodes( $content ), true);
[/code]

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
[code]<?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 );

?>[/code]

bbcode.php
[code]<?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>',
  '[/code]' => '</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]
[code=php:0]echo "my text or tutorial here, in this format.";[/code]
[/codetag][/quote]

hopefully, you'll understand the problem i have, and come up with a soloution. thanks.
Link to comment
https://forums.phpfreaks.com/topic/4907-highlighting-code/#findComment-17605
Share on other sites

I was just saying that instead of writing all the code as "[code=php:0] ~ [/code]", "<?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

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

That will work for $code where [code=php:0][/code] 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.
Link to comment
https://forums.phpfreaks.com/topic/4907-highlighting-code/#findComment-18005
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.