[solved] php highlight function help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

aaronhall wrote:I think this thread's about due for an updated summary, Kieran ;)
That's the only reason I am still posting in it.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i am using preg_replace_callback() but when i use the highlighting feature all it shows is

Code: Select all

1   Array
even though i am trying to parse '<?php phpinfo(); ?>' how would i fix this?
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i dont know why it only highlights "Array"... it only says array in the bbcode function, and i am not even using that to highlight the code... any help?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

It prints array because you are trying to use the array of matches (which is passed you to hc function) as a string.

Code: Select all

echo '<pre>';
print_r($code);
Put this inside your hc function. This will show you what your function is actually given.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

now it prints
Array
(
[0] =>

Code: Select all

    [1] => 
)

PHP Code

1   Array
[/quote]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

New summary:

:?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead:
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

I feel up to speed now. Thanks, Kieran
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

can someone help me on why it my code wont work??? :banghead: :banghead: :banghead: :banghead: :banghead: :banghead:
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Oh! My god I'vent seen Such a looooooooooooong post still now.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

ok i found better code...
here is bbcode.php

Code: Select all

<?php
function bbcode($string){
$string = nl2br(htmlspecialchars($string));
$patterns = array(
                   '`\[b\](.+?)\[/b\]`is',
                   '`\[i\](.+?)\[/i\]`is',
                   '`\[u\](.+?)\[/u\]`is',
                   '`\[strike\](.+?)\[/strike\]`is',
                   '`\[color=#([0-9]{6})\](.+?)\[/color\]`is',
                   '`\[email\](.+?)\[/email\]`is',
                   '`\[img\](.+?)\[/img\]`is',
                   '`\[url=([a-z0-9]+://)([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\](.*?)\[/url\]`si',
                   '`\[url\]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]`si',
                   '`\[url\]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]`si',
                   '`\[flash=([0-9]+),([0-9]+)\](.+?)\[/flash\]`is',
                   '`\[quote\](.+?)\[/quote\]`is',
                   '`\[size=([1-6]+)\](.+?)\[/size\]`is'
                   );

$replaces =  array(
                      '<strong>\\1</strong>',
                      '<i>\\1</i>',
                      '<u>\\1</u>',
                      '<strike>\\1</strike>',
                      '<span style="color:#\1;">\2</span>',
                      '<a href="mailto:\1">\1</a>',
                      '<img src="\1" alt="" style="border:0px;" />',
                      '<a href="\1\2">\6</a>',
                      '<a href="\1\2">\1\2</a>',
                      '<a href="http://\1">\1</a>',
                      '<object width="\1" height="\2"><param name="movie" value="\3" /><embed src="\3" width="\1" height="\2"></embed></object>',
                      '<strong>Quote:</strong><div style="margin:0px 10px;padding:5px;background-color:#F7F7F7;border:1px dotted #CCCCCC;width:80%;"><em>\1</em></div>',
                      '<h\1>\2</h\1>'
                   );
$string = preg_replace($patterns, $replaces , $string);
return $string;
?>
and here is highlight.php

Code: Select all

<?php
function php($string){
$code_count = preg_match_all( '#\[php\](.*?)\[\/php\]#si', $string, $matches);

for( $i = 0; $i < $code_count; $i++ )
    {
        $replaced = $matches[1][$i];
     $replaced = hc( htmlspecialchars2( $replaced ));
         return $replaced;
    }
}

function htmlspecialchars2( $text )
{
    static $patterns, $replaces;
     if( !$patterns ){
        $patterns = array( '#<#', '#>#', '#&#', '#"#' );
        $replaces = array( '<', '>', '&', '"' );
    }
    return preg_replace( $patterns, $replaces, $text );
}

?>
i got these codes from here
http://www.isolated-designs.net/core/pr ... -functions

how would i combine those 2 together? i tried but it only does one or the other. can you just tell me the code from highlight.php that i should put in bbcode.php :)
Last edited by ziggy3000 on Wed Apr 25, 2007 5:35 pm, edited 1 time in total.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i tried putting this code

Code: Select all

$code_count = preg_match_all( '#\[php\](.*?)\[\/php\]#si', $string, $matches);

     $string = hc( htmlspecialchars2($string));
         return $string;
    }
but the result is

Code: Select all

<i>test2</i>[php]<?php phpinfo(); ?>[/php] <strong>test</strong>
but it highlights the php code.

should i use preg_replace_callback() or what should i do?

(my original input was

Code: Select all

[i]test2[/i][php]<?php phpinfo(); ?>[/php] [b]test[/b]
)
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

does anyone know the solution to my problem???

this is me-> :banghead: :banghead: :banghead: :banghead: :banghead: :banghead:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

your passing $string when you should be passing $matches[1] me thinks.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

which part are you talking about...(i posted so much code, im confused :roll: )
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I was referring to the last code you posted.
Post Reply