Link to home
Start Free TrialLog in
Avatar of linuxrox
linuxroxFlag for United States of America

asked on

rewriting links or redirect links.

Hello.  I have a file that i download from associated press site and all of the links start out as follows:

<A HREF=http://hosted.ap.org

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?
Avatar of jstretch
jstretch

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.

http://us4.php.net/manual/en/function.ereg-replace.php

<?

$page = include('http://hosted.ap.org/some_page.php");

$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.
Avatar of linuxrox

ASKER

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

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

$regex="<a href=\"[-_.a-zA-Z0-9]+\">[-_.a-zA-Z0-9]+</a>";
Ok, i'll give that a try.  i solved the problem just using string functions as follows:

$data3 = "http://www.myurl.com/myfile";
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')){
    }else{
    $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=http://www.link_i_desire_to_replace_with.com>", $data3);
     echo $data4;
}      
    fclose($file);  
}  
?>

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.
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial