Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 07-18-2007, 11:42 AM
user02934123123 user02934123123 is offline
 
Join Date: Apr 2006
Location: UK
Posts: 91
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Passing and parsing variables through functions in a postbit mod

Hello all,

I'm an older coder, but relatively new at vB/PHP hacks, so am running into odd problems that I'm not certain of. Any assistance greatly received!

Here is my current situation: I've created a hack that runs in postbit, and uses regex to find phrases in a post which, if matched with terms entered in a vb-options setting of dictionary terms, hyperlinks them and links them to a dictionary file.

All is working well, save for one problem: the regex code finds terms that are not internal to words (e.g. finding 'bin' on its own, but not in the middle of 'albino' by looking for terms followed by a space or a non-letter character. Here is the regex (which presumes a number of terms are already in a variable called $terms_regex, e.g. 'bin|trash|dog'):

Code:
$terms_regex='/(?:('.$terms_regex.')([\s$_]|[^a-rt-z0-9]))/';
As such, that works fine. However, by determining whole words by finding trailing spaces or non-letters (e.g. commas, full stops, etc.), it includes these in the actual terms - so it matches "bin " or "bin," and not simply the word itself.

Here is the code that texts the regex and replaces located terms with linked versions:

Quote:
$terms_regex='/(?'.$terms_regex.')([\s$_]|[^a-rt-z0-9]))/';

$reference = '\\0';

if ( $vbulletin->options['mdict_newwin'] == 1 ) {
// $link = '<a target="_blank" class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
$link = '<a target="_blank" class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
} else {
// $link = '<a class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
$link = '<a class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
}

$text=preg_replace($terms_regex,$link,$text);
Again, this works as such; but the linked terms include linking of the whole regex-located term, which includes the trailing space or punctuation. What I need is a way to trim off the last character of the expression when creating the link.

I've tried doing this by using rtrim() on $reference - but this doesn't work -- I think because the actual contents of that variable are a mystery to me. They are received from the line of code I've put in boldface, above (namely: $reference = '\\0'. This is a snippet of code that I gleaned from looking at other vB hacks; but its actual nature is a mystery to me. How precisely does it work? What kind of variable is it producing? If I have the script echo it onto the page, it correctly displays the word (e.g. 'bin'); but if I then do an if..then... on the variable (e.g. "if ($reference == 'bin')"), it fails (!).

I would appreciate - greatly - any assistance, as I admit I'm rather stumped.

Many thanks!

NB: For fuller reference, below is the fuller code of the hack I'm working on, so you can see where the above code snippets lie and how they're being called:

Quote:
<plugin active="1" executionorder="5">
<title>Convert to Monachos Dictionary Link</title>
<hookname>global_start</hookname>
<phpcode><![CDATA[



function makedict ($text = ''){
// Skip everything within a hyperlink, a <pre> block, a <code> block, or a tag
// We skip tags because something like <img src="blah" alt="John 3:16"> should not be messed with
$anchor_regex = '<a\s+href.*?<\/a>';
$pre_regex = '<pre>.*<\/pre>';
$code_regex = '<code>.*<\/code>';
$tag_regex = '<(?:[^<>\s]*)(?:\s[^<>]*){0,1}>'; // $tag_regex='<[^>]+>';
$split_regex = "/((?:$anchor_regex)|(?:$pre_regex)|(?:$code_regex)| (?:$tag_regex))/i";

$parsed_text = preg_split($split_regex,$text,-1,PREG_SPLIT_DELIM_CAPTURE);
$linked_text = '';

while (list($key,$value) = each($parsed_text)) {
if (preg_match($split_regex,$value)) {
$linked_text .= $value; // if it is an HTML element or within a link, just leave it as is
} else {
$linked_text .= makedictAddLinks($value); // if it's text, parse it for Monachos Dictionary linking
}
}

return $linked_text;
}

function makedictAddLinks($text='') {
global $vbulletin;

if ( $vbulletin->options['mdict_terms1'] != '' ) {
$terms_regex = $vbulletin->options['mdict_terms1'];
} else {
$terms_regex = 'Monachos.net';
}
if ( $vbulletin->options['mdict_terms2'] != '' ) {
$terms_regex .= '|' . $vbulletin->options['mdict_terms2'];
}
if ( $vbulletin->options['mdict_terms3'] != '' ) {
$terms_regex .= '|' . $vbulletin->options['mdict_terms3'];
}

$terms_regex='/(?'.$terms_regex.')([\s$_]|[^a-rt-z0-9]))/';

$reference = '\\0';


if ( $vbulletin->options['mdict_newwin'] == 1 ) {
// $link = '<a target="_blank" class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
$link = '<a target="_blank" class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
} else {
// $link = '<a class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
$link = '<a class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
}

$text=preg_replace($terms_regex,$link,$text);

return $text;
}


]]></phpcode>
</plugin>
<plugin active="1" executionorder="5">
<title>Convert to Monachos Dictionary Link</title>
<hookname>postbit_display_complete</hookname>
<phpcode><![CDATA[
$post['message']= makedict($post['message']);
]]></phpcode>
</plugin>
Reply With Quote
Reply

Thread Tools
Display Modes

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 04:20 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.10910 seconds
  • Memory Usage 2,209KB
  • 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)bbcode_code
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)showthread_list
  • (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_threadedmode.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_threaded
  • showthread_threaded_construct_link
  • 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
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete