View Full Version : Preg_Replace with Links?
AdamCap
12-22-2011, 08:32 PM
Here's the plugin I'm working on:
Product: vBulletin
Hook Location: postdata_presave
Order: 5
$search[] = '/\b(Term1)\b/';
$replace[] = '<a href="http://somewhere.com/" class="some-class" target="_blank">$1</a>';
$this->post['pagetext'] = preg_replace($search, $replace, $this->post['pagetext'], 1);
The replace works, however the HTML is just spit out and not parsed. I realize that BB code would work, but I need to style the link with CSS.
Any ideas on how to make this work?
Lynne
12-23-2011, 02:40 AM
If it's in the postbit, how about using hook postbit_display_complete
HMBeaty
12-23-2011, 02:57 AM
More of a description as to what exactly you're trying to do would help as well :)
AdamCap
12-23-2011, 02:43 PM
@Lynne: It doesn't seem to work; the terms aren't being replaced, but I appreciate the suggestion! :)
postbit_display_start seems to work as far as getting the search/replace to work, but the links aren't parsed.
@HMBeaty: Sorry, I should have made it clearer:
I'm trying to link specific keywords within posts using preg_replace(). Full story: I run a Pokemon Card website, so I'm trying to automatically link card names to a database website so people can quickly look them up. I made a plugin for my WordPress portion of the site which works great, and I'm trying to port it over to vB.
I need to use CSS to style the links to make them look different than normal hyperlinks for convenience of my readers, so they can differentiate the types of links quickly.
nhawk
12-23-2011, 02:53 PM
These 3 things might help..
1) Try doing it in the bbcode_parse_start hook.
2) Clear $parsedtext by using $parsedtext = '' before your replacements. If you don't do that, the posts will always show what's in the postparsed cache.
3) Use the URL BB Code for the replacement, not HTML <a href.
The word will automatically become a link in the post which should make it stand out from the rest.
AdamCap
12-23-2011, 03:15 PM
Thanks for the ideas nhawk!
I tried bbcode_parse_start but that didn't work, and $parsedtext = '' also seemed to have no effect. The only success I've had so far with getting the replacements to work has been with these hooks:
postdata_presave
threaddata_presave
postbit_display_start
I may just switch to BB Code rather than try and get the HTML to work... it seems like it should be easy enough to figure this out. I just have so little experience coding in vB.
Also by stand out, I mean I want the linked terms to stand out from other links within posts. I use a more subtle link styling on my WordPress install since a bunch of terms might be mentioned in a post, and it looks messy with the default link styling.
Lynne
12-23-2011, 04:44 PM
I have used preg_replace to successfully create links using the postbit_display_complete hook.
$word = array(
'My Link'
);
$link = array(
'<a href="link.php">My Link</a>'
);
$this->post['message'] = str_ireplace($word, $link, $this->post['message']);
AdamCap
12-23-2011, 05:12 PM
Rock on!!! Thanks so much Lynne!
I think the issue I had before when I tried postbit_display_complete was that I was using:
$this->post['pagetext'] = preg_replace($search, $replace, $this->post['pagetext'], 1);
Instead of:
$this->post['message'] = preg_replace($search, $replace, $this->post['message'], 1);
Needed 'message' instead of 'pagetext' I guess. Thanks again!
Wedframe
04-19-2015, 07:24 AM
Sorry for bumped old thread...
I try to make replace url`s from our own clowd into image with this url.
Then I use hook postbit_display_complete with this code:
$cloudurl = "/" .substr("\[url\]http:\/\/wedframe\.ru\/downloads\.php\?do=file&id=\d*&act=down\[\/url\]", 0). "/";
$testcloudurl = preg_match($cloudurl, $this->post['message'], $matchecloudurl);
$iconicurl = ", 5, -6). "\"]https://vborg.vbsupport.ru/external/2015/04/16.png (\"" .substr($matchecloudurl[0)";
$this->post['message'] = str_ireplace($matchecloudurl[0], $iconicurl, $this->post['message']);
its wont work. Because of url tag`s, I think?..
If I use hook postdata_presave with slightly changed code:
$cloudurl = "/" .substr("\[url\]http:\/\/wedframe\.ru\/downloads\.php\?do=file&id=\d*&act=down\[\/url\]", 0). "/";
$testcloudurl = preg_match($cloudurl, $this->post['pagetext'], $matchecloudurl);
$iconicurl = ", 5, -6). "\"]https://vborg.vbsupport.ru/external/2015/04/16.png (\"" .substr($matchecloudurl[0)";
$this->post['pagetext'] = str_ireplace($matchecloudurl[0], $iconicurl, $this->post['pagetext']);
replacing work correctly... but I have a problem with moderated posts. With enabled this module, approved posts saved with cleared itself content.
So... I`m not deeper coder of vB, and I hope somebody help me with correct code for my module.
Thanks anyway!
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.