Jump to content

Replace hyperlinks destination


milesperhour1086

Recommended Posts

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??
Link to comment
Share on other sites

[!--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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.