preg_replace_all() related issue

Aristona picture Aristona · Jan 4, 2012 · Viewed 7.1k times · Source

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.

Answer

Ashley Banks picture Ashley Banks · Jan 4, 2012

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....