Jump to content

Recommended Posts

OK, i am not too sure how this works and i am looking for some advice. I want to add a [player] [/player] BBcode.

'/\[player\](.*?)\[\/player\]/is', 

Now, i want to convert the player name into the players ID by querying my database. I will then create a link <a=(link)(user_id)>(playername)</a>

Does anyone have any idea how i would do this and where i put my db query?

Can this even be done with my current script?

[code]<?php
function bbcode_format ($str) {
global $dbc;
$str = $str;

$simple_search = array(
'/\[b\](.*?)\[\/b\]/is',                               
'/\[i\](.*?)\[\/i\]/is',                               
'/\[u\](.*?)\[\/u\]/is',                               
'/\[url\=(.*?)\](.*?)\[\/url\]/is',                       
'/\[url\](.*?)\[\/url\]/is',                           
'/\[align\=(left|center|right)\](.*?)\[\/align\]/is',   
'/\[image\](.*?)\[\/image\]/is',                           
'/\[email\=(.*?)\](.*?)\[\/email\]/is',                   
'/\[email\](.*?)\[\/email\]/is',                           
'/\[font\=(.*?)\](.*?)\[\/font\]/is',                   
'/\[size\=(.*?)\](.*?)\[\/size\]/is',                   
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
'/\[player\](.*?)\[\/player\]/is',       
);

$query = "SELECT user_id FROM unc_users WHERE username='$1'";
$result = mysql_query ($query)or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error());

$simple_replace = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
'<a href="$1">$2</a>',
'<a href="$1">$1</a>',
'<div style="text-align: $1;">$2</div>',
'<img src="$1" />',
'<a href="mailto:$1">$2</a>',
'<a href="mailto:$1">$1</a>',
'<span style="font-family: $1;">$2</span>',
'<span style="font-size: $1;">$2</span>',
'<span style="color: $1;">$2</span>',
'<strong>$1</strong>',
);

// Do simple BBCode's
$str = preg_replace ($simple_search, $simple_replace, $str);

// Do <blockquote> BBCode
$str = bbcode_quote ($str);

return $str;
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/32958-editting-this-bbcode/
Share on other sites

You will want to create a function say call it getPlayer. You will then call this function from your regex when a patch is found. When a match is found you will give the getPlayer function the id for the player. The function will then query the databases for the player id and return the player name. When the player name is returned it will then create the link and return the link back.

You will want to modify your player regex slightly to this:
[tt]/\[player\](.*?)\[\/player\]/ise[/tt]

You may note the addition of the [b]e[/b] modifier at the end of the regex. The e modifier is used so you can execute PHP code in the replacement parameter. So this is what you'll want to do. Change [tt]'<strong>$1</strong>',[/tt] from the smple_replace array to this:
[code]"getPlayer('$1')"[/code]

Now add the following function to your script:
[code=php:0]function getPlayer($playerID)
{
    $qry = "SELECT username FROM unc_users WHERE user_id='$playerID' LIMIT 1";
    $result = mysql_query($qry);

    $player = mysql_fetch_assoc($result);

    $playerLink = '<a href="player.php?pid=' . $playerID . '">' . $player['username'] . '</a>';

    return $playerLink;
}[/code]

You may need to change the query ($qry) to your needs and modify the player link too.
Link to comment
https://forums.phpfreaks.com/topic/32958-editting-this-bbcode/#findComment-153648
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.