Page 1 of 1

parsing posts

Posted: Fri Jul 18, 2003 2:16 pm
by Fredix

Hi I'm looking for a way to parse text "dynamically" for a kind of BBcode.
In my guestbook you can specify the text color like on this board and then something like [ color = #HEXHEX ] (without spaces) appears. Now I need a regex or something to look for [ color = ] and replace it with < font..... > using the #HEXHEX value. Has anyone an idea how to do such a thing?
thank you

Posted: Fri Jul 18, 2003 4:56 pm
by AVATAr
search in this forum, there are many solutions to that kind of problem

Posted: Fri Jul 18, 2003 6:54 pm
by Gen-ik
It's easier to have a set range of colours.... and not every knows how to use HEX colour values.

Code: Select all

<?php

$text = "this is [col=red]red[/col] text"; // just here as test text

str_replace("[col=red]", "<font color=ff0000>", $text);
str_replace("[/col]","</font>", $text);

echo($text);

?>

Posted: Sat Jul 19, 2003 5:16 am
by Fredix
@Gen-ik
yes that would be easier but not what I want, because (if you know what hex colors are) you can use any color even if it is not listed in the drop down box and I would like to do a smart regex and not a "brute force" comparison.

@Avatar
I searched for a while but didn't quite find the answer. Mainly my problem is not finding the [color=#0-1a-fA-f] but to convert it to

Code: Select all

<font style="color:#HEXHEX">

Posted: Sun Jul 20, 2003 6:47 am
by Fredix
no one has an idea? :cry:

Posted: Sun Jul 20, 2003 8:42 am
by Gen-ik
You could always try a hard 'n rough approach to your problem.

You'll have to split apart the submitted text and take things from there.. here's an example.

Code: Select all

<?php
$the_text = "Hello my [col=ff0000]name[/col] is [col=0000ff]bob[/col]";

//split the text apart using [col=
$splitA = explode("[col=", $the_text);

//this will give you an array with the hex values at the start of each value.
//this is what the array for the above text will look like..
//
// $splitA[0] = "Hello my "
// $splitA[1] = "ff0000]name[/col] is "
// $splitA[2] = "0000ff]bob[/col]"
//
//now insert html at the start of each hex value starting from $splitA[1];

for($i=1; $i<count($splitA); $i++)
{
$splitA[$i] = "<font color=".$splitA[$i];
//replace [/col] with the closing html tag
$splitA[$i] = str_replace("[/col]","</font>",$splitA[$i]);
//replace the remaining ] with >
$splitA[$i] = str_replace("]",">",$splitA[$i]);
}

// now join everything back together

$new_text = "";

for($i=0; $i<count($splitA); $i++)
{
$new_text .= $splitA[$i];
}

echo($new_text);
?>
This should work.... if not then it will give you an idea of how to go about doing it.

Posted: Sun Jul 20, 2003 9:40 am
by Drachlen
Im not sure if this would work in every instance, but its alot less code, and it might be easier to understand if you're newer to php.. One thing i was having problems with was passing # through the url, but it does work, i tested it multiple times, i just dont know how to pass #..

Code: Select all

<?php
$text = str_replace("[","<",$text);
$text = str_replace("col","font",$text);
$text = str_replace("="," color=",$text);
$text = str_replace("]",">",$text);
echo "$text";
?>
Im sure you could do the same thing more effiecently with less str_replace, but thats just the way i came up with..

Posted: Mon Jul 21, 2003 12:30 pm
by Fredix
thank you very much.

It was easier than I thought but there is still a very little problem:

what to do if someone writes ] within his own text (for some reason). Actually this is parsed into a ">!

btw: that's the code I'm using now:

Code: Select all

<?php
$eintrag = str_replace("[color=", "<span style="color:", $eintrag);
$eintrag = str_replace("[/color]", "</span>", $eintrag);
$eintrag = str_replace("]", "">", $eintrag);

?>

Posted: Mon Jul 21, 2003 6:41 pm
by McGruff
You can pass arrays to str_replace, so for example:

Code: Select all

<?php
// define global vars:
$BBcode_search = array('[ b]', '[ /b]' .. etc);
$BBcode_replace = array('< b>', '< /b>' .. etc); // spaces added to avoid display problem here

function BBcode(&$string) {

    $string = str_replace($GLOBALS['BBcode_search'], $GLOBALS['BBcode_replace'], $string);
}

// in use
$string = ' .. long string with BB code tags .. ';
BBcode($string);

?>
Add as many BB code tags as you like to the search/replace arrays - just remember that search key X is replaced with replace key X.

To decode (eg when you are editing a post) write another fn with the search/replace arrays swapped around.

[Admin Edit: kindly admin fixed parse error]

Posted: Tue Jul 22, 2003 4:29 am
by Fredix
ehm. so what?

(PS. in your php code there is a ' missing in line 3.)