• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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?
  • 3
  • 2
1 Solution
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('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.
linuxroxAuthor Commented:
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);

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

$regex="<a href=\"[-_.a-zA-Z0-9]+\">[-_.a-zA-Z0-9]+</a>";
linuxroxAuthor Commented:
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')){
    $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;

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.
linuxrox. Have a look at this code:

  <a href="http://hosted.ap.org">test</test>
  <a href=http://hosted.ap.org target=_blank>test</test>
  <A HREF=http://hosted.ap.org/test>test</test>
  <img src="http://hosted.ap.org" />
  $c = ob_get_clean();

  $cn = preg_replace('#href=(["]?)http://hosted.ap.org#i', 'href=\\1http://mydomain.com/redirect.php?http://hosted.ap.org', $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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now