View Full Version : Trying to do replacements
dontpanic
01-28-2004, 12:34 AM
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:$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!
g-force2k2
01-28-2004, 01:18 PM
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
dontpanic
01-28-2004, 01:35 PM
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.$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... :)
NTLDR
01-28-2004, 01:43 PM
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>
dontpanic
01-28-2004, 01:47 PM
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. :)
g-force2k2
01-28-2004, 01:48 PM
dontpanic couldn't you nest your replacement codes in characters that are illegal in urls, characters that the urlencode function changes.
for example:
{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
NTLDR
01-28-2004, 01:52 PM
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:
$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 ;)
dontpanic
01-28-2004, 01:56 PM
dontpanic couldn't you nest your replacement codes in characters that are illegal in urls, characters that the urlencode function changes.
for example:
{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. :)
dontpanic
01-28-2004, 01:57 PM
Its posible to tell it to look for one thing but not another, its allong the lines of:
$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?
g-force2k2
01-28-2004, 01:59 PM
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
NTLDR
01-28-2004, 02:02 PM
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.
dontpanic
01-28-2004, 02:03 PM
Right. Now down to business. I will see what I can come up with given this great start! :)
dontpanic
01-28-2004, 02:14 PM
Its posible to tell it to look for one thing but not another, its allong the lines of:
$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, this may be close, but PHP is seeing the </a>as an unknown "a" modifier.
NTLDR
01-28-2004, 02:18 PM
$var = preg_replace('/(tofind)^(<a href="\S">tofind<\/a>)/i', 'replacement', $var);
Whoops, forgot the \ before the \/a that will solve that error :)
dontpanic
01-28-2004, 02:24 PM
I figured that. :)
Anyhow, I have inserted my information into the code as seen:$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']);This works great in the fact that it causes no errors. This does not work great in the fact that it doesn't replace anything. :ermm: Now we know why these regex's are such a pain in the butt!
I'll get back on this shortly. Gotta go do some paying work now.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.