Jump to content


Photo

Replace hyperlinks destination


  • Please log in to reply
5 replies to this topic

#1 milesperhour1086

milesperhour1086
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 April 2006 - 08:28 PM

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:

<a href="mailto:someone@somewhere.com">Text goes here</a>

into the following

<a href="<?=$offset?>/mail.php?email=someone@somewhere.com">Text goes here</a>

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??
Micah[br] --PHP Newbie

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 April 2006 - 08:48 PM

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?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 milesperhour1086

milesperhour1086
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 April 2006 - 08:50 PM

[!--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) View Post[/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.
Micah[br] --PHP Newbie

#4 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 28 April 2006 - 09:22 PM

this would be the regex for it
ereg_replace("<a.*mailto:(.*)".*>(.*)</a>", "<a href="<?=$offset?>/mail.php?email=\\1">\\2</a>", $matches);

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#5 milesperhour1086

milesperhour1086
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 April 2006 - 06:14 PM

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:

    <!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);

    ?>

OUTPUTS ONLY THE FOLLOWING:

    <!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>

Micah[br] --PHP Newbie

#6 milesperhour1086

milesperhour1086
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 April 2006 - 08:09 PM

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:

<?

$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();
    
?>

Micah[br] --PHP Newbie




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users