Hi guys
Im creating a league site for a gamming competiton. The database has a list clans with an ID. I thn have a news posting script which allows the input of BB code with the tag [cl]clanidhere[/cl]. The idea behind this is that in the news posting you can just input the bb code refering to a clan id and then when view the text it will replace the bb code with the clans name and crountry.
Ive already done basic bb code and smilie bb code. But this one is proving to be a challenge. You can have multiple [cl] tags in a news post sp u cant simple do a str_replace, so im guessing i need to loop through the amount of times i have a [cl] tag and replace each one individually.
Im not exactly sure how to do this.
If anyone could enlighten me i would greatly appriciate the help
here is an example of a news post with the bb code (and spelling errors):
Hi guys
Well unfortunantly another team has dropped from our league. [cl]5[/cl] have dropped out due to internal problems. They will now be replaced by [cl]3[/cl]. Give the new team a warm welcome tp the league.
So i would need to get the Clan ID's 5,3 so i can then run my functions to get clan details and then replace the bb code.
Thanks
Krak3n
String Manipulation Problem
Moderator: General Moderators
here ya go
it will not work right if your bbcode is malformed though, like forgetting to close the tag [/cl]
output:
Well unfortunantly another team has dropped from our league. ---I have been parsed!(old=[cl]5[/cl])--- have dropped out due to internal problems. They will now be replaced by ---I have been parsed!(old=[cl]3[/cl])---. Give the new team a warm welcome tp the league.
it will not work right if your bbcode is malformed though, like forgetting to close the tag [/cl]
Code: Select all
<?php
function extract_tags($document)
{
$tags = array();
$tag_count = substr_count($document, '[cl]');
$pointer = 0;
for ($i=0; $i<$tag_count; $i++) {
$open_pos = strpos($document, '[cl]', $pointer);
$close_pos = strpos($document, '[/cl]', $open_pos) + 5;
$tag_length = $close_pos - $open_pos;
$pointer = $close_pos;
$tags[] = substr($document, $open_pos, $tag_length);
}
return $tags;
}
$input = 'Well unfortunantly another team has dropped from our league.
[cl]5[/cl] have dropped out due to internal problems. They will now be
replaced by [cl]3[/cl]. Give the new team a warm welcome tp the league.';
$tags = extract_tags($input);
foreach ($tags as $tag) {
$old = $tag;
$new = '---I have been parsed!(old='.$tag.')---'; // do you substitution here
$input = str_replace($old, $new, $input);
}
echo $input;
?>Well unfortunantly another team has dropped from our league. ---I have been parsed!(old=[cl]5[/cl])--- have dropped out due to internal problems. They will now be replaced by ---I have been parsed!(old=[cl]3[/cl])---. Give the new team a warm welcome tp the league.