PDA

View Full Version : scan for bbcodes within posts


succo
06-05-2007, 07:28 AM
ok, this is what i'd like to do:
i've got a nice 'media' forum, containing many bbcode tags for stuff i'd like to show in the homepage... at the moment i'm working on making the 'upcoming' module read all the posts in some forums, find the ones containing 'my' bbcode and strip it and put it in a variable... this is my code so far:

<?php
require_once('./global.php'); // for now it's working alone :)
$mod_options['code'] = "media"; // this is my media code
$mod_options['limit'] = "5"; // i want only five records
$mod_options['forumid'] = "35"; // the forum where the media stuff is
if (!$mod_options['replace']) {
$mod_options['replace'] = $mod_options['code'];
}
$poststuffs = $db->query_read_slave("
SELECT post.pagetext
FROM " . TABLE_PREFIX . "post AS post
INNER JOIN " . TABLE_PREFIX . "thread USING (threadid)
WHERE forumid = ".$mod_options['forumid']."
AND pagetext LIKE '%[".$mod_options['code']."]%[/".$mod_options['code']."]%'
AND pagetext NOT LIKE '%[".$mod_options['code']."]%mp3[/".$mod_options['code']."]%'
AND pagetext NOT LIKE '%[".$mod_options['code']."]mms%[/".$mod_options['code']."]%'
AND pagetext NOT LIKE '%%[".$mod_options['code']."]%[/".$mod_options['code']."]%%'
ORDER BY post.dateline DESC LIMIT ".$mod_options['limit']."
");
while ($poststuff = $db->fetch_array($poststuffs)) {
$poststring[] = ereg_replace("\[/".$mod_options['code']."\].*$", "[/".$mod_options['code']."]", ereg_replace("^.*\[".$mod_options['code']."\]", "[".$mod_options['replace']."]", $poststuff[pagetext])); // remove everything before and after my tag
}
print_r($poststring); // i want to see what happened :)
?>

it's partially working, as it finds the correct posts, but (in one case) doesn't succeed in stripping some part of the post away, maybe (that's what i noticed) because it finds some " (quotes)

may anyone help in building this thing up? :)

Dismounted
06-05-2007, 07:49 AM
Use preg instead of ereg. It's faster.

succo
06-05-2007, 08:20 AM
mmmmmmmh... trying with $seek = array("/^.*\[".$mod_options['code']."\]/", "/\[\/".$mod_options['code']."\].*$/");
$destroy = array("[".$mod_options['replace']."]", "[/".$mod_options['code']."]");
$poststring[] = preg_replace($seek, $destroy, $poststuff[pagetext]); but isn't substituting anything in the text... so, what's wrong? :)
thanks

Dismounted
06-05-2007, 09:05 AM
Have you had a look at how vBulletin does it?

succo
06-05-2007, 09:24 AM
ok, will try later when back from work :)

thanks

ok, made it work like this

<?php
require_once('./global.php');
$mod_options['code'] = "media";
$mod_options['limit'] = "5";
$mod_options['forumid'] = "35";
if (!$mod_options['replace']) {
$mod_options['replace'] = $mod_options['code'];
}
$poststuffs = $db->query_read_slave("
SELECT post.pagetext
FROM " . TABLE_PREFIX . "post AS post
INNER JOIN " . TABLE_PREFIX . "thread USING (threadid)
WHERE forumid = ".$mod_options['forumid']."
AND pagetext LIKE '%[".$mod_options['code']."]%[/".$mod_options['code']."]%'
AND pagetext NOT LIKE '%[".$mod_options['code']."]%mp3[/".$mod_options['code']."]%'
AND pagetext NOT LIKE '%[".$mod_options['code']."]mms%[/".$mod_options['code']."]%'
AND pagetext NOT LIKE '%%[".$mod_options['code']."]%[/".$mod_options['code']."]%%'
ORDER BY post.dateline DESC LIMIT ".$mod_options['limit']."
");
while ($poststuff = $db->fetch_array($poststuffs)) {
$poststring[] = preg_replace("#^.*\[".$mod_options['code']."\](.*)\[/".$mod_options['code']."\].*$#si", "[".$mod_options['replace']."]$1[/".$mod_options['code']."]", $poststuff[pagetext]);
}
print_r($poststring);
?>
thanks :)