parsing posts

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

Post Reply
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

parsing posts

Post 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
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

search in this forum, there are many solutions to that kind of problem
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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);

?>
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post 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">
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

no one has an idea? :cry:
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post 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..
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post 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);

?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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]
Last edited by McGruff on Thu Aug 11, 2005 1:04 am, edited 1 time in total.
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

ehm. so what?

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