PDA

View Full Version : Help me finish my next hack


Logikos
10-28-2004, 10:11 AM
Hey guys, I haven't been online much since the recent move I did. Don't have much computer access where I'm staying at the moment, but I can't live without my computer and got really bored so I started to dig threw old request threads and saw someone requested a hack to turn a specified 'text' in a post and replace it with a 'url/link'. Well I looked around and didn't find it so I was rather happy that I could make a new hack cause I really didn't know what to do on the computer with no internet, was getting bored. ^^

Anyways, I started to think how I can do this, and it seemed pretty simple actually, just make a new table in the database named 'textlink' with a few needed fields such as 'textlinkid', 'text', and 'link' to hold the info. So I completely finish the admincp part of the hack.

Now the next part was to make it read all the text in each post and check to see if any of the text posted needed to be replaced with a link. This again seemed rather simple to me. I figure I could use the str_replace() function built into PHP. This is where I got a dead end.

Inside that function I need to it read the database field 'text' and replace it with the the info in the database field 'link'. Here's what I think it will look like if I'm not mistaking.

$post['message'] = str_replace( $textvar, linkvar, $post['message'] );

I believe I need to execute a query to define what to look for, and to define the variables that will be inside the str_replace() function. When I made a query it wouldn't read the info and replace it. Can anyone help me finish this hack? Of course credit definitely goes to everyone who helps. Thanks guys.

Database information:
-Textlink (table name)
---textlinkid (field name for the id of the text/link)
---text (field name for the text to replace)
---link (field name for the url to replace the text with)

Thanks in advance everyone!

Xenon
10-28-2004, 06:38 PM
do you just replace for newposts or are you changing the bbcode parsing functions?

depending on your answer you'd have to use caching or not of course :)

as for the general:


$textlinks = $DB_site->query("SELECT * FROM Textlink");
$find = array();
$replace = array();
while ($link = $DB_site->fetch_array($textlinks))
{
$find[] = $link['text'];
$replace[] = ' . ']' . $link['text'] . ' (' . $link['link')';
}
$post['message'] = str_replace($find, $replace, $post['message']);

as said, that's the general idea, actually using preg_replace would be better but mor complicated, as it would also allow you to ignore cases and you have to make sure not to replace text which is already a code or url or something like ;)

Logikos
10-28-2004, 09:51 PM
Hey Xenon, again thanks again! I was planing on using the preg_replace() function, but like you said. Its damm complacated. I'm reading about it now in my PHP book. ^^.

Xenon
10-29-2004, 04:11 PM
You're welcome :)

and yeah, play around with the preg_replace. i avoided it in the past as well, as it's very complicated but once you found out the little tricks it's a very strong tool :)

Logikos
10-29-2004, 06:36 PM
I'm not sure the best place to add this code. I was thinking about doing it in the includes/functions_bbcodeparse.php file and do the query and this:


$bbcode = str_replace($find, $replace, $bbcode);


Which works just fine acually, but like you said str_replace() won't take care of the caps and all. So i'm still reading and trying to figure out the preg_replace() function.

Xenon
10-30-2004, 12:27 PM
putting it into the parse_bbcode function makes it required to cache your $find/$replace arrays, as if you don't do it, you would produce an extra query per post on every page which would be a big no no ^^

Logikos
10-30-2004, 11:31 PM
putting it into the parse_bbcode function makes it required to cache your $find/$replace arrays, as if you don't do it, you would produce an extra query per post on every page which would be a big no no ^^
Damm i wish i would have read your post before, cause thats what i did and i did notice that every post adds a query but can't figure it out. Maybe you can help me, i posted it as a beta hack and posted all known bugs that i have found. https://vborg.vbsupport.ru/showthread.php?t=71191

Thank man!

Xenon
10-31-2004, 11:43 AM
The answer is already posted there, and it should work the way he posted afaics :)