parsing posts
Moderator: General Moderators
- Fredix
- Forum Contributor
- Posts: 101
- Joined: Fri Jul 18, 2003 2:16 pm
- Location: Wehr (Eifel) Germany
- Contact:
parsing posts
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
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);
?>- Fredix
- Forum Contributor
- Posts: 101
- Joined: Fri Jul 18, 2003 2:16 pm
- Location: Wehr (Eifel) Germany
- Contact:
@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
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">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.
This should work.... if not then it will give you an idea of how to go about doing it.
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);
?>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 #..
Im sure you could do the same thing more effiecently with less str_replace, but thats just the way i came up with..
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";
?>- Fredix
- Forum Contributor
- Posts: 101
- Joined: Fri Jul 18, 2003 2:16 pm
- Location: Wehr (Eifel) Germany
- Contact:
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:
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);
?>You can pass arrays to str_replace, so for example:
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]
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);
?>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.