milesperhour1086 Posted April 28, 2006 Share Posted April 28, 2006 I am trying to add a new function to our intranet website where, upon loading a page in your web browser, a php script would run that would convert this:[code]<a href="mailto:someone@somewhere.com">Text goes here</a>[/code]into the following[code]<a href="<?=$offset?>/mail.php?email=someone@somewhere.com">Text goes here</a>[/code]This would only apply to the mailto links and no other links on the page. Any ideas anyone??Would this be something that I can have run "on-the-fly" or is it something that I would have to run server-side?? Quote Link to comment https://forums.phpfreaks.com/topic/8662-replace-hyperlinks-destination/ Share on other sites More sharing options...
.josh Posted April 28, 2006 Share Posted April 28, 2006 umm well you could use pregmatch or substr to look for the "mailto:" in the link and then convert from there. is that what you are looking for? Quote Link to comment https://forums.phpfreaks.com/topic/8662-replace-hyperlinks-destination/#findComment-31787 Share on other sites More sharing options...
milesperhour1086 Posted April 28, 2006 Author Share Posted April 28, 2006 [!--quoteo(post=369706:date=Apr 28 2006, 01:48 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Apr 28 2006, 01:48 PM) [snapback]369706[/snapback][/div][div class=\'quotemain\'][!--quotec--]umm well you could use pregmatch or substr to look for the "mailto:" in the link and then convert from there. is that what you are looking for?[/quote]Yeah...that's the idea I'm going for and I knew about those functions...basically, if I could put something in the header that would SEARCH on that page for any links that contains mailto: in it and change it, that would be what I want. We've got a huge site and I would rather not go page by page adding a function call to each and every link. Quote Link to comment https://forums.phpfreaks.com/topic/8662-replace-hyperlinks-destination/#findComment-31788 Share on other sites More sharing options...
Zane Posted April 28, 2006 Share Posted April 28, 2006 this would be the regex for it[code]ereg_replace("<a.*mailto:(.*)".*>(.*)</a>", "<a href="<?=$offset?>/mail.php?email=\\1">\\2</a>", $matches);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8662-replace-hyperlinks-destination/#findComment-31798 Share on other sites More sharing options...
milesperhour1086 Posted April 29, 2006 Author Share Posted April 29, 2006 It has been recommended to use an output buffer and create my ereg function within that so I did but the problem is that I get the following to happen:Starting with this code:[code] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <? function replaceMailLink($buffer) { $buffer = ereg_replace("<a.*mailto.*)\".*>(.*)</a>", "<a href=\"$offset/mail.php?email=\\1\">\\2</a>", $buffer); return $buffer; } ob_start(); ?> </head> <body> <a href="www.test.com">This is a test</a> <a href="mailto:someone@somewhere.com">Micah's E-mail</a> </body> </html> <? $buffer = ob_get_contents(); ob_end_clean(); echo replaceMailLink($buffer); ?>[/code]OUTPUTS ONLY THE FOLLOWING:[code] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <a href="/mail.php?email=someone@somewhere.com">Micah's E-mail</a> </body> </html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8662-replace-hyperlinks-destination/#findComment-31999 Share on other sites More sharing options...
milesperhour1086 Posted April 29, 2006 Author Share Posted April 29, 2006 Here is a much cleaner version of the source code from above...it still only prints the ONE mailto: link and not the other dummy link:[code]<?$offset = "."; function callback($buffer) { global $offset; return (ereg_replace("<a.*href=\"mailto:(.*)\".*>(.*)</a>", "<a href=\"".$offset."/mail.php?email=\\1\">\\2</a>", $buffer)); } ob_start("callback");?><html><head><title>Testing...</title></head><body><p><a href="www.cfddtacoma.org">Link #1 - No Email</a></p><p><a href="mailto:milesperhour1086@gmail.com">Link #2 - Email </a></p></body></html><? ob_end_flush(); ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8662-replace-hyperlinks-destination/#findComment-32027 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.