Help - Search - Members - Calendar
Full Version: [php] bbcode function
Weborum Webmaster Forum > Web Page Design > PHP
Joe
My function as it stands aims to upgrade all the normal highlight_string() tags and bring them into the 21st century with valid XHTML 1.0 strict codes.

Using a function I tried to buid using 2 other functions I have run into a problem trying to integrate them together;

http://us3.php.net/manual/en/function.highlight-string.php

PHP
<?php
function bbcode($s)
{
   
$s = str_replace("]\n", "]", $s);
   
$match = array('#\[php\](.*?)\[\/php\]#se');
   
$replace = array("'<div>'.highlight_string(stripslashes('$1'), true).'</div>'");
   return
preg_replace($match, $replace, $s);
}
?>


And

PHP
<?php
function xhtml_highlight($str) {
   
$str = highlight_string($str, true);
   
$str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
   return
preg_replace('#style="color:(.*?)"#', 'style="color: \\1"', $str);
}
?>


My function at the moment is;

PHP
function xhtml($str) {
   $str = highlight_string($str, true);
   // $str = substr($str, 29, -15);
   $str = str_replace(array('<font ', '</font>','<code>','</code>'), array('<span ', '</span>','<div class="php-code">','</div>'), $str);
   return preg_replace('#style="color:(.*?)"#', 'style="color: \\1"', $str);
}


Which is just highlighting the entire content, I want to only highlight the code between the [ php ] tags, everything I try to do this doesn't work so I was wondering if someone could help me a lil'.

Cheers smile.gif
aviansuicide
way to steal my example functions and not be able to modify them, mate ^_~

PHP

<?
function xhtml_highlight($str) {
   
$str = highlight_string($str, true);
   
$str = str_replace(array('<font ', '</font>','<code>','</code'), array('<span ', '</span>','<div class="php-code">','</div>' ), $str);
   return
preg_replace('#style="color:(.*?)"#', 'style="color: \\1"', $str);
}
?>



PHP

<?
function bbcode($s)
{
   
$s = str_replace("]\n", "]", $s);
   
$match = array('#\[php\](.*?)\[\/php\]#se');
   
$replace = xhtml_highlight('\\1');
   return
preg_replace($match, $replace, $s);
}
?>

Joe
QUOTE(aviansuicide @ Apr 8 2005, 10:03 PM)
way to steal my example functions and not be able to modify them, mate ^_~


I didn't steal your functions blink.gif

Both examples that I tried to combine were from;
http://us3.php.net/manual/en/function.highlight-string.php

I got it working in the end with some extra bb tags added on.

PHP
<?php
function bbcode($string)
{
   
$string = preg_replace('~\[i\](.*?)\[\/i\]~is', '<i>\\1</i>', $string);
   
$string = preg_replace('~\[b\](.*?)\[\/b\]~is', '<strong>\\1</strong>', $string);
   
$string = preg_replace('~\[u\](.*?)\[\/u\]~is', '<u>\\1</u>', $string);
   
$string = str_replace("]\n", "]", $string);
   
$match = array('#\[php\](.*?)\[\/php\]#se');
   
$replace = array("'<div class=\"php-code&#092;">'.highlight_string(stripslashes('$1'), true).'</div>'");
   
$string = preg_replace($match, $replace, $string);
   
$font = array('<font ', '</font>','<code>','</code>');
   
$span = array('<span ', '</span>','','');
   
$string = str_replace($font, $span, $string);
   
$string = preg_replace('#style="color:(.*?)"#', 'style="color: \\1"', $string);
   return
$string;
}
?>


... but as usual, one problem goes and another one arises rolleyes.gif

Cheers for the help though Will ... should have posted back saying I'd solved it really blush.gif
aviansuicide
I was just teasing you mate...remember the discussion we had like..a month ago..about xhtml compliant string highlighting?I showed you an almost duplicate function that I was using. You know I'd never 'seriously' accuse you of stealing code...if I showed you it..your welcome to use it. XD

Glad you got it fixed tho.

h198848
I heard there is some javascript can do this. I dont know is that ture?
Joe
Theres JavaScript to insert the tags yes, but to parse and change them into HTML code it uses a server side language smile.gif
aviansuicide
QUOTE(h198848 @ May 23 2005, 07:55 AM)
I heard there is some javascript can do this. I dont know is that ture?
[right][snapback]28366[/snapback][/right]


Javascript can't parse the tags...they need to be parsed on the server -- and sent to the browser as their proper html counterparts. Javascript is commonly used, as in the posting page for this forum...to trigger buttons which insert the code.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.