2

I am currently making a BBCode class for my website. I need to do the following.

function bbCode([skill]Q[/skill]_________[skill]Q[/skill]_________[skill]Q[/skill]);

bbCode function has to replace all Q's between [skill] and [/skill] tags, and replace it with $skillArray['Q'] value. ($skillArray is character dependant.)

How can I do this?

A little clearer version is:

  1. For example you're on a page of "Orc" character.
  2. [skill]Q[/skill] tag should get "Orc's Q skill name" automatically.
  3. For example you're on a page of "Hunter" character.
  4. [skill]Q[/skill] tag should get "Hunter's Q skill name" automatically.

Ps. Don't want to use explode.

Aristona
  • 8,611
  • 9
  • 54
  • 80

3 Answers3

1

Assuming the tags you want to be replaced are within some form of template, you could use file_get_contents and then loop through the tags you want to replace with the desired values, for example:

$file = file_get_contents ( 'yourfile.php' );

$skillArray = array ( 'Q' => 'Hunter name' );

foreach ( $skillArray as $key => $val )
{
    $file = str_replace ( '[skill]' . $key . '[\skill]', $val, $file );
}

Thats just a completely rough example, but if I'm understanding what you are trying to do; that should be along the right lines....

Ashley Banks
  • 528
  • 5
  • 16
  • This will loop only once due to only one value in array. There can be 15x [skill] Q [/skill] BBCode in a comment. It will only replace the first then.

    I want it to replace all of them in once.
    – Aristona Jan 04 '12 at 10:13
  • Yeah thats fine, this was just an example of how you could implement it. You could put as many items in the array as you wanted, and it would loop for that time... – Ashley Banks Jan 04 '12 at 10:18
  • It should work I agree, but I was expecting it to do with different function like preg_replace_all(). Doing it with str_replace in a for loop isn't the thing I wanted. :) – Aristona Jan 04 '12 at 10:20
  • @AnılÜnal he just gave the idea. You need to do it yourself. – Shiplu Mokaddim Jan 04 '12 at 10:21
  • @Shiplu Yeah, I know, but there is no array. There is only a string. I can't really do anything with his suggestion. :) – Aristona Jan 04 '12 at 10:31
  • You will need an array of some kind to determine what to 'skill' to replace with what value... – Ashley Banks Jan 04 '12 at 10:35
1

This is what you need

$data = "[skill]Q[/skill]_________[skill]Q[/skill]_________[skill]Q[/skill]";
$r['Q'] = "Yahoo";
function b($a){
    global $r;
    return $r[$a[1]];
}
$data = preg_replace_callback('|\[skill\](Q)\[\/skill\]|', 'b' , $data);
var_dump($data);

If you want to replace all the Qs with single "Yahoo" use Q+ instead of Q. If you want to match all words use \w+.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Could you explain the B function? Didn't see a function being called like this before.

    Does preg_replace_callback automatically calls the function of second string parameter?
    – Aristona Jan 04 '12 at 10:25
  • This is a call back function that is called with *matches* as parameter. – Shiplu Mokaddim Jan 04 '12 at 19:18
1
<?php
$skillArray=array('Q'=>'fishing');
$txt="test [skill]Q[/skill]  test";
$txt=preg_replace("#\[skill\](.*)\[\/skill\]#e",'$skillArray["$1"]',$txt);
echo $txt; //test fishing test
?>