Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 12-22-2011, 08:32 PM
AdamCap AdamCap is offline
 
Join Date: Dec 2009
Posts: 19
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Preg_Replace with Links?

Here's the plugin I'm working on:

Product: vBulletin
Hook Location: postdata_presave
Order: 5

PHP Code:
$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?
Reply With Quote
  #2  
Old 12-23-2011, 02:40 AM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

If it's in the postbit, how about using hook postbit_display_complete
Reply With Quote
  #3  
Old 12-23-2011, 02:57 AM
HMBeaty's Avatar
HMBeaty HMBeaty is offline
 
Join Date: Sep 2005
Posts: 4,141
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

More of a description as to what exactly you're trying to do would help as well
Reply With Quote
  #4  
Old 12-23-2011, 02:43 PM
AdamCap AdamCap is offline
 
Join Date: Dec 2009
Posts: 19
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

@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.
Reply With Quote
  #5  
Old 12-23-2011, 02:53 PM
nhawk nhawk is offline
 
Join Date: Jan 2011
Posts: 1,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #6  
Old 12-23-2011, 03:15 PM
AdamCap AdamCap is offline
 
Join Date: Dec 2009
Posts: 19
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 12-23-2011, 04:44 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I have used preg_replace to successfully create links using the postbit_display_complete hook.

PHP Code:
$word = array( 
'My Link' 
); 

$link = array( 
'<a href="link.php">My Link</a>'
); 


$this->post['message'] = str_ireplace($word$link$this->post['message']); 
Reply With Quote
Благодарность от:
AdamCap
  #8  
Old 12-23-2011, 05:12 PM
AdamCap AdamCap is offline
 
Join Date: Dec 2009
Posts: 19
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Rock on!!! Thanks so much Lynne!

I think the issue I had before when I tried postbit_display_complete was that I was using:

PHP Code:
$this->post['pagetext'] = preg_replace($search$replace$this->post['pagetext'], 1); 
Instead of:

PHP Code:
$this->post['message'] = preg_replace($search$replace$this->post['message'], 1); 
Needed 'message' instead of 'pagetext' I guess. Thanks again!
Reply With Quote
  #9  
Old 04-19-2015, 07:24 AM
Wedframe Wedframe is offline
 
Join Date: Mar 2013
Posts: 48
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:
PHP 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 "[URL=\"" .substr($matchecloudurl[0], 5, -6). "\"][img]https://vborg.vbsupport.ru/external/2015/04/16.png[/img][/URL]";

$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:
PHP 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 "[URL=\"" .substr($matchecloudurl[0], 5, -6). "\"][img]https://vborg.vbsupport.ru/external/2015/04/16.png[/img][/URL]";

$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!
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:11 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04744 seconds
  • Memory Usage 2,264KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (6)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (1)post_thanks_box_bit
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete