rewriting links or redirect links.

Posted on 2005-05-09
Last Modified: 2006-11-18
Hello.  I have a file that i download from associated press site and all of the links start out as follows:


what i desire is on the page that displays these links, for the links to either not be clickable or for me to be able to redirect the clicks to a page within my site.  i can't really rewrite the links because the links contain images from the AP site that wouldn't show if we rewrote them.  ijust need to be able to, once the links are shown to someone in the browser, to redirect a click or something.  i'm trying to prevent people from actually being able to click the displayed links and getting the story yet still show them the main headlines.
anyone have any ideas?
Question by:linuxrox
    LVL 6

    Expert Comment

    You'd probably have to create a ereg expression that would find the links and replace them with whatever link you wanted. I'm not very knowledgable about ereg's but I"m sure someone can whip one up for you.


    $page = include('");

    $pattern = "create reg expression here"; //this is a search patter to find certain strings, in your case, links likee <a href=hosted.ap.....

     echo ereg_replace ( $pattern, "<a href=yourlink.php>your link</a>, $page);


    Perhaps I can figure out the express you need.

    Author Comment

    probably regular expressions yes...also, i was incorrect.  i just need to be able to change the links between the <a href and the >
    the image references can remain the same actually.  so basically i need to be able to find the string "<A HREF" then find the next ">" adn replace everything between that with what i choose.  then echo that back out to the browser.
    that should take care of it.
    LVL 6

    Expert Comment

    This is the closest I've gotten, cant get it working with parenthesis however.


    $string = "<a href=link.html>link</a>"; //you'd replace this with your page ofcourse
    $replace = "<a href=\"test.html\">test</a>"; //your link
    $regex="<a href=+[>_a-zA-Z.]+</a>";

    echo ereg_replace($regex,$replace,$string);

    LVL 6

    Expert Comment

    I think this does it, atleast on what I tested:

    $regex="<a href=\"[-_.a-zA-Z0-9]+\">[-_.a-zA-Z0-9]+</a>";

    Author Comment

    Ok, i'll give that a try.  i solved the problem just using string functions as follows:

    $data3 = "";
    if (!$file = fopen($data3, 'rb')) {
        echo 'Error whilst opening the file.';
    } else {
        $data3 = '';
        while (!feof($file)) {
        $data3 = fgets($file);
        if(!$start = strpos($data3, '<A HREF')){
        $startstring = substr($data3, $start);
        $stringlen1 = substr($data3, 0, $start);
        $strlen = strlen($stringlen1);  
        $startnewstring = strpos($startstring, '>');
        $totalstringlength = $startnewstring+$strlen;
        $finalstring = substr($data3, 0, $totalstringlength + 1);
        $newpos = strpos($finalstring,'<A HREF');
        $finalstring = substr($finalstring, $newpos);
         $data4 = str_replace($finalstring, "<A HREF=>", $data3);
         echo $data4;

    this effectively replaces the <A HREF string until the next >  with what i wish.  of course i think this would only work if there is just one occurrence of the string on one line.  not sure what would happen with two <A HREF's on the same line.
    LVL 49

    Accepted Solution

    linuxrox. Have a look at this code:

      <a href="">test</test>
      <a href= target=_blank>test</test>
      <A HREF=>test</test>
      <img src="" />
      $c = ob_get_clean();

      $cn = preg_replace('#href=(["]?)', 'href=\\1', $c);
      echo '<h1>old</h1>';
      echo nl2br(htmlspecialchars($c));
      echo '<h1>new</h1>';
      echo nl2br(htmlspecialchars($cn));
    preg_replace is also much faster than ereg_replace. However we might have to adjust the code some little more to also take <a href = " and such into account.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now