Jump to content


BBcode


4 replies to this topic

#1 stingerblue

    Young Padawan

  • Members
  • Pip
  • 16 posts
  • Gender:Male
  • Location:North of the Border, UK

Posted 26 August 2007 - 05:20 PM

I have searched, but couldnt find anything, I dont know how to explain it, its basically like emoticons that are stored in a database, plus using this type of code for say like user page urls, avatars, pictures etc:

$change = "[user_stingerblue];
$from = '[user_(.*)]';

Then its checked for illegal characters, then its queried, if true it returns back with a url.

Thank you in advance.

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 26 August 2007 - 08:29 PM

Just the basic principals of custom BBcode. Basically you can just alter a normal BBCode and inject it with some extra steps.
You've got two options. You can either pre-load all your member's names into an array from the beginning, and use simple regex and replace, or you can dynamically take them out and create a query and find them at the end, then replace them.
I'll show you the latter since I think that would be more efficient resource-wise, so you're not loading every single member into an array that you may be searching for only 2 for.
<?php
// Our initial string, just a dummy string here, lol
$string = '[user=Demonslay] is awesome. [user=stingerblue] might be just as awesome once he learns PHP. :P';
// Grab our tags
preg_match_all('#\[user=([^\[]+)\]#', $string, $matches);
// Custom function to escape our values
function escape(&$str){
	$str = "'".mysql_real_escape_string($str)."'';
}
array_walk($matches[1], 'escape');
// Implode and form a query to select the found usernames
$users = '`username` = '.implode(' OR `username` = ', $matches[1]);
$query = 'SELECT * FROM `users` WHERE '.$users;
// Execute query
$results = mysql_query($query) or die(mysql_error());
// Loop through and replace the strings - if you aren't running PHP5+ you may want to change this to str_replace() or find an alternative for str_ireplace() to get no case-sensitivity
while($user = mysql_fetch_array($results)){
	$string = str_ireplace('[user='.$user['username'].']', '<a href="/profile.php?user='.$user['id'].'">'.$user['username'].'</a>', $string);
}
// And we'll see how we did
echo $string;
?>
I've run this with some dummy data and it works just fine.
Should give you a good idea how that works. :)

#3 Wybe

    Jedi In Training

  • Members
  • PipPip
  • 399 posts
  • Gender:Male
  • Location:the Netherlands
  • Interests:Graphic design, digital and traditional, street style, graffiti, guerilla drawing, typography, coding, sex

Posted 27 August 2007 - 06:59 AM

A useful piece of code Demonslay! I've never really understood the whole preg_match syntax and all

#4 stingerblue

    Young Padawan

  • Members
  • Pip
  • 16 posts
  • Gender:Male
  • Location:North of the Border, UK

Posted 27 August 2007 - 08:31 AM

Wow thank you Demonslay :D lol.

#5 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 27 August 2007 - 03:22 PM

No problem. :)

Oh, and I spotted a minor bug with my regex; it seems to work nevertheless, but it may run into problems sometime down the road.
I simply put the wrong bracket symbol in the collector.
Simply replace the preg_match_all() line with this one.
preg_match_all('#\[user=([^\]]+)\]#', $string, $matches);






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users