Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 01-28-2004, 12:34 AM
dontpanic dontpanic is offline
 
Join Date: Jun 2003
Posts: 145
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Trying to do replacements

I'm trying to use the preg_replace command to do replacements of specific strings with the string as a URL. An example from what I am using:
PHP Code:
$post['message'] = preg_replace('/(70-292)/i''<a href="http://www.mcseworld.com/a/1932266569/" target="_blank">\\1</a>'$post['message']); 
This code would thus case the 70-292 string to be replaced with that URL.

I have two issues:

1. I cannot quite figure out how to get ONLY exact matches, no matter where they might appear in the post. I've been reading on this here: http://www.php.net/manual/en/function.preg-replace.php and here: http://www.php.net/manual/en/pcre.pattern.modifiers.php

2. I know that I will need a if/then loop to prevent the substitution from being made if the string is already encased in <a> or [URL] tags, but I am not sure how to go about that either.

I am currently using this in the functions_showthread.php file for vB 3.0.0 RC3, so no actual changes are being made to the data as the subs are done on the display end of things.

Thanks in advance!
Reply With Quote
  #2  
Old 01-28-2004, 01:18 PM
g-force2k2 g-force2k2 is offline
 
Join Date: Mar 2002
Location: Everywhere you wanna be..
Posts: 1,608
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

dontpanic,

Could you perhaps use either str_replace or substr_replace functions instead to get the effect you're looking for?

I haven't really done much with preg_replace so I can't really give you much assistance there.

Cheers,
g-force2k2
Reply With Quote
  #3  
Old 01-28-2004, 01:35 PM
dontpanic dontpanic is offline
 
Join Date: Jun 2003
Posts: 145
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

g-force2k2,

This code below does have the same effect, but my real problem is figuring out how to prevent it from replacing in any <a> or [URL] instances.
PHP Code:
$post['message'] = str_replace("70-292""<a href=\"http://www.mcseworld.com/a/1932266569/\" target=\"_blank\">70-292</a>"$post['message']); 
I am looking to perform the replacement conditionally, only if the string is not already linked.

You've got me looking in another direction though...
Reply With Quote
  #4  
Old 01-28-2004, 01:43 PM
NTLDR's Avatar
NTLDR NTLDR is offline
Coder
 
Join Date: Apr 2002
Location: Bristol, UK
Posts: 3,644
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Note that str_replace() is case sensitive, so for the words for example it will only match exact case and you can't tell it to replace abc and not <a href="x">abc</a>
Reply With Quote
  #5  
Old 01-28-2004, 01:47 PM
dontpanic dontpanic is offline
 
Join Date: Jun 2003
Posts: 145
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

NTLDR,

Good point. I am having the same problems with str_replace() as I did with preg_replace() when we talked last. I have to figure out a way to prevent the replacement from occuring in existing <a> and [URL] instances as the replacement will break them and some times create interesting output as well.

Thanks both for your inputs.
Reply With Quote
  #6  
Old 01-28-2004, 01:48 PM
g-force2k2 g-force2k2 is offline
 
Join Date: Mar 2002
Location: Everywhere you wanna be..
Posts: 1,608
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

dontpanic couldn't you nest your replacement codes in characters that are illegal in urls, characters that the urlencode function changes.

for example:

Code:
{70-292}
I don't think that the brackets would be used or can be used in urls, but I could be wrong.

That way you don't have to worry about parsing through the urls or a hrefs.

Also take a look at the functions_bbcodeparse.php might give you some ideas.

Regards,
g-force2k2
Reply With Quote
  #7  
Old 01-28-2004, 01:52 PM
NTLDR's Avatar
NTLDR NTLDR is offline
Coder
 
Join Date: Apr 2002
Location: Bristol, UK
Posts: 3,644
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by dontpanic
NTLDR,

Good point. I am having the same problems with str_replace() as I did with preg_replace() when we talked last. I have to figure out a way to prevent the replacement from occuring in existing <a> and [URL] instances as the replacement will break them and some times create interesting output as well.
Its posible to tell it to look for one thing but not another, its allong the lines of:

PHP Code:
$var preg_replace('/(tofind)^(<a href="\S">tofind</a>)/i''replacement'$var); 
Now I'm certain the above, won't work , however its along those lines
Reply With Quote
  #8  
Old 01-28-2004, 01:56 PM
dontpanic dontpanic is offline
 
Join Date: Jun 2003
Posts: 145
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by g-force2k2
dontpanic couldn't you nest your replacement codes in characters that are illegal in urls, characters that the urlencode function changes.

for example:

Code:
{70-292}
I don't think that the brackets would be used or can be used in urls, but I could be wrong.

That way you don't have to worry about parsing through the urls or a hrefs.

Also take a look at the functions_bbcodeparse.php might give you some ideas.

Regards,
g-force2k2
g-force2k2, yes this does appear to work quite well actually. It's a bit awkward from the point of view that you'd have to remember to encase it using {}, but it certainly does work.
Reply With Quote
  #9  
Old 01-28-2004, 01:57 PM
dontpanic dontpanic is offline
 
Join Date: Jun 2003
Posts: 145
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by NTLDR
Its posible to tell it to look for one thing but not another, its allong the lines of:

PHP Code:
$var preg_replace('/(tofind)^(<a href="\S">tofind</a>/i''replacement'$var); 
Now I'm certain the above, won't work , however its along those lines
NTLDR, I will have to dig into this more. Question about your syntax though. Just to avoid any confusion, can you explain where I'd put the string to search for and the string that is to be placed in the output?
Reply With Quote
  #10  
Old 01-28-2004, 01:59 PM
g-force2k2 g-force2k2 is offline
 
Join Date: Mar 2002
Location: Everywhere you wanna be..
Posts: 1,608
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by dontpanic
NTLDR, I will have to dig into this more. Question about your syntax though. Just to avoid any confusion, can you explain where I'd put the string to search for and the string that is to be placed in the output?
I'm pretty interested in the preg_replace syntax too, doesn't help to learn more.

Regards,
g-force2k2
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 07:06 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04611 seconds
  • Memory Usage 2,260KB
  • 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
  • (2)bbcode_code
  • (4)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)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
  • 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
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete