![]() |
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:
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! |
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 |
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:
You've got me looking in another direction though... :) |
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>
|
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. :) |
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} 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 |
Quote:
PHP Code:
|
Quote:
|
Quote:
|
Quote:
Regards, g-force2k2 |
I only know abit about it, regular expressions aren't my strong point.
@dontpanic: tofind is the search code, like survivor and replacement is the code to replace it with, I just used those as they were shorter. The ^ before the bracket should negate whats in it so if it matches the first part, but not the second then it does the replacement. At least thats how you want it too work. |
Right. Now down to business. I will see what I can come up with given this great start! :)
|
Quote:
Code:
</a> |
PHP Code:
|
I figured that. :)
Anyhow, I have inserted my information into the code as seen: Code:
$post['message'] = preg_replace('/(70-292)^(<a href="\S">70-292<\/a>)/i', '<a href="http://www.mcseworld.com/a/1932266569/" target="_blank">\\1</a>', $post['message']); I'll get back on this shortly. Gotta go do some paying work now. |
All times are GMT. The time now is 01:26 AM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|