Jump to content

Need BBCode Help


destrugter

Recommended Posts

Ok, I am making my own forums, and I want to make it to where when the user puts something in, such as [user posts=destrugter] it connects to my database and grabs my posts, and displays them in the post...not the actual posts, the amount of posts...then if I post something after that post it will automatically update with the database. Here is the code I have so far (just ignore the basic bbcode tags).

<?phpfunction BBCodes($str) {		$str = htmlentities($str);		$simple_search = array(								'/\[b\](.*?)\[\/b\]/is',																'/\[i\](.*?)\[\/i\]/is',																'/\[u\](.*?)\[\/u\]/is',								'/\[url\=(.*?)\](.*?)\[\/url\]/is',								'/\[smiley\=(.*?)\]/is',								'/\[user posts\=(.*?)\]/is'								);		$simple_replace = array(								'<strong>$1</strong>',								'<em>$1</em>',								'<u>$1</u>',								'<a href="$1">$2</a>',								'<img src="../imgs/smilies/$1.gif"></img>',								Get_Posts("$1")								);		// Do simple BBCode's		$str = preg_replace($simple_search, $simple_replace, $str);		return $str;}function Get_Posts($Username) {$result = mysql_query("SELECT * FROM `Users` WHERE `Username` = '". $Username ."'");	while($row = mysql_fetch_array($result))	{return $row['Posts'];}}?>

I am not going to actually allow users to post that...it's just a simple thing I started off with to make sure I know how it works for the bigger project. Can someone please tell me what I did wrong?I have the post as:Destrugter has: [user posts=destrugter] posts.When it goes through and replaces for the BBCodes it does this:Destrugter has: posts.But when I remove the where section from the mysql_query, it grabs the first username to appear and that works...but I don't want the first user, I want the one specified by the poster.

Link to comment
Share on other sites

You're trying to execute the function immediately and send it $1 as an argument, which is a backreferece for the regular expression engine. The backreference doesn't get processed and converted when you execute the function though, so you're literally sending the function the text "$1", not the match that was found (you could have found that out if you printed the username that was sent to the function to check it). You can only use the code you have if you are replacing one string with another one. If you need to do some processing based on the match then you can't use that code, you need a separate piece of code that only looks for the one thing and if it finds matches then it sends the matches to the function, gets the result, and then does the replacement.

Link to comment
Share on other sites

It will work if you are replacing one string with another string. If you are doing anything else, if you have to build the string from another source, then it's not going to work to try and preg_replace the matches with the output from the function. Look at what it does, when you do this:

		$simple_replace = array(								'<strong>$1</strong>',								'<em>$1</em>',								'<u>$1</u>',								'<a href="$1">$2</a>',								'<img src="../imgs/smilies/$1.gif"></img>',								Get_Posts("$1")								);

You define the replacement array, and you execute the Get_Posts function immediately, as soon as you do that. You haven't even ran the regular expression match yet and you already ran the Get_Posts function. After defining that array if you do this:print_r($simple_replace)You'll see all of those lines like they are defined there, plus an empty element that is the result of the Get_Posts function, which got executed as soon as you created the $simple_replace array. You want to do the match first, and then use the result for the Get_Posts function. That is why they named the variables $simple_search and $simple_replace, because they are for simple situations. If you want to do something more complex with the matches then you can't use preg_replace. You need to use preg_match first to get the matches, then do your processing on the matches, then do the replacement. preg_replace just replaces the matches with the replacement strings and substitutes the matches into the places where the backreferences are defined (the $1, $2, etc).If you want to replace it with a URL string with the username there, that's fine, then you're just replacing one string with another string.

Link to comment
Share on other sites

Ok, I'll look up those commands you explained later...i can't right now because my mom wants to get on the computer and I don't have time...thanks a ton though, you are the first, out of all of the other forums I posted in to help me.

Link to comment
Share on other sites

Ok, Here is what I came up with so far last night (I haven't yet gotten to look up preg_match but I will I assure you my website and games are some of the most important things to me(besides family, friends, and school of course), and if something will improve either of them I will go to any length to do it). I found out this works (I think this is sort of what you were telling me all along programming and computer science are the highest of all of my passions)...but it doesn't allow the other text before and after the tag to show up. I will try to figure this out, but here is the code I came up with so far.

<?php include("../includes/connection.php"); ?><?phpfunction BBCodes($str) {		$str = htmlentities($str);		$simple_search = array(								'/\[b\](.*?)\[\/b\]/is',																'/\[i\](.*?)\[\/i\]/is',																'/\[u\](.*?)\[\/u\]/is',								'/\[url\=(.*?)\](.*?)\[\/url\]/is',								'/\[smiley\=(.*?)\]/is',								'/\[username\](.*?)\[\/username\]/is'								);		$simple_replace = array(								'<strong>$1</strong>',								'<em>$1</em>',								'<u>$1</u>',								'<a href="$1">$2</a>',								'<img src="../imgs/smilies/$1.gif"></img>',								$Username = '$1'								);		// Do simple BBCode's		$str = preg_replace($simple_search, $simple_replace, $str);		return Get_Posts($str);}function Get_Posts($Username) {$result = mysql_query("SELECT * FROM `Users` WHERE `Username` = '". $Username ."'");	while($row = mysql_fetch_array($result))	{return $row['Posts'];}}?>

Link to comment
Share on other sites

It's going to be best to use a different function for the username stuff. Leave the normal replace stuff for the basic strings, and have a separate function to do the special things. So first you need to do a match to find the username stuff, you can use preg_match_all for that. If you run this code, you'll see the following output:

<?php$pattern = '/\[username\](.*?)\[\/username\]/is';$matches = array();$subject = "adklfj alskjf laewjkflseiaosjdkfmle kfal kfj sldkfjaeiofjakld [username]test1[/username] akdsfj aejfklea;kldfj sl;dkfjaaskldlakljsf akljdfal ldskfj jlej ladskjf [username]test2[/username] aend9s d9d de3ndjsld djlisos [username]test3[/username].";$num = preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);if ($num != 0){  echo "found {$num} matches:\n";  print_r($matches);}else  echo "no matches found";?>

found 3 matches:Array(	[0] => Array		(			[0] => [username]test1[/username]			[1] => test1		)	[1] => Array		(			[0] => [username]test2[/username]			[1] => test2		)	[2] => Array		(			[0] => [username]test3[/username]			[1] => test3		))

So it returns a multidimensional array of matches. Each element in the array is one set of matches. The first element in the set is the entire pattern that matched (which you're going to replace), and the second element is the part that matched the first subpattern, or the actual user's name. So then you can loop through the matches array, do your database query, and replace the match with the result. So after the code above you can do this:

foreach ($matches as $match){  $uname = mysql_real_escape_string($match[1]);  $result = mysql_query("SELECT `Posts` FROM `Users` WHERE `Username` = '{$uname}'");    if ($row = mysql_fetch_assoc($result))	$repl = $match[1] . ' has ' . $row['Posts'] . ' posts'; // whatever you want to replace it with  else	$repl = '';  // do the replacement  $subject = str_ireplace($match[0], $repl, $subject);}

Link to comment
Share on other sites

All I can say is wow...you are amazing at this stuff...you must be very valuble in the computer science industry. Don't worry, I'm not one of those "copy and pasters" I just copied and pasted to figure out if it would work, and I'm currently starting from scratch and following the guidelines of your code. I believe in your sig too, well I'm off for now, if I have anymore questions I obviously know where to go now =PThanks a ton justsomeguy!

Link to comment
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...