Log in

View Full Version : preg_replace in functions_newpost.php help


tfw2005
01-30-2006, 05:08 PM
Help with a preg_replace call in includes/functions_newpost.php

I currently have a simple preg_replace function working, not in the board system.

I would like to apply it to all new posts before they are inserted into the DB.

This is the original preg_replace


<?php

$string = "http://subdomain1.domain1.com/";
$find = "/http:\/\/subdomain1\.domain1\.com\//";
$replace = "http://www.domain.com/out.php?out=http://subdomain1.domain1.com/";

print preg_replace($find, $replace, $string);

?>


I would like to apply that logic to any instance of http://subdomain1.domain1.com/ that is present in a users post, then enter it into the DB.

I tried the following to no avail in includes/function_newpost.php



// ### POST NEW POST ###


$string = "$post['message']";
$find = "/http:\/\/subdomain1\.domain1\.com\//";
$replace = "http://www.domain.com/out.php?out=http://subdomain1.domain1.com/";

$post['message'] = preg_replace($find, $replace, $string);


$DB_site->query("
INSERT INTO " . TABLE_PREFIX . "post

............................... etc


Any idea how to get that to apply? I saw an amazon affiliates hack that did it, but couldnt get it to work.

Would also be great if the logic could make it so that the link shown is the original link, while the acutal url= part has the rewritten code. For asthetic purposes, because these links can get rather long.


Thanks for the help.

Paul M
01-30-2006, 09:54 PM
What version of vb is this referring to ?

tfw2005
01-30-2006, 10:59 PM
3.03

Paul M
01-31-2006, 02:37 AM
In that case sorry, I no longer have 3.0, so I don't have the code handy anymore.

Xenon
01-31-2006, 03:54 PM
not sure if i got you absolutelly right, but if i did, than this line should be the only one you need:

$post['message'] = preg_replace(
'#(http:\/\/[a-z0-9]+\.[a-z0-9]+\.[a-z0-9]+\/)#i',
'\1 (http://www.domain.com/out.php?out=\1)',
$post['message']
);

tfw2005
01-31-2006, 06:07 PM
Hey Xenon,

Thanks for the help. That worked, almost.

If i remove the URL BBCode part of your replace section, it works perfect, with no rewrite for the displayed link.

If I leave it in, then I get a double attempt at URL BBcode.

Right now, I took what you posted, and removed that, so I just get a shortened version of the full link, including the initial http://www.domain.com/out.php?out=

Magic wand situation would be if someone types without any BBCODE

http://subdomain1.domain1.com/(variable, whatever EX home.php or blahblahblah/)

this is generated (Look at link via hover)


http://subdomain1.domain1.com/(variable) (http://www.domain.com/out.php?out=http://subdomain1.domain1.com/variable))



And, in theory, if someone posts it with bbcode (in the case of editing a post), the same thing happens.

I can live with this now though, just people see that there is an outbound tracker attached to these specific links.