2

I've run into a hard problem to deal with. I am replacing a-tags and img-tags to fit my suggestions like this. So far so good.

$search = array('|(<a\s*[^>]*href=[\'"]?)|', '|(<img\s*[^>]*src=[\'"]?)|');
$replace = array('\1proxy2.php?url=', '\1'.$url.'/');
$new_content = preg_replace($search, $replace, $content);

Now my problem is that there are links on pages that i fetch the content of that looks like this:

<a href="/test/page/">

and

<a href="http://google.se/test/">

And when after replacing these two links looks like this:

<a href="proxy2.php?url=/test/page/">

and

<a href="proxy2.php?url=http://google.se/test/">

The problem is for me is that i want to include a variable named $url before /test/page/ and only on that links that are like that, not those who was already http:// or https:// before.

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
  • +1 Never saw the greeting "Yo!" on SO before and because you're new :) – Henrik P. Hessel Aug 09 '09 at 19:57
  • Possible duplicates: http://stackoverflow.com/questions/1251859/php-replace-question and http://stackoverflow.com/questions/1254890/pregreplacecallback-do-twice – Gumbo Aug 10 '09 at 18:50

4 Answers4

2

This should do the job for the anchor tags, at least:

<?php
function prepend_proxy($matches) {
    $url = 'http://example.prefix';

    $prepend = $matches[2] ? $matches[2] : $url;
    $prepend = 'proxy2.php?url='. $prepend;

    return $matches[1] . $prepend . $matches[3];
}
$new_content = preg_replace_callback(
    '|(href=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
    'prepend_proxy',
    $content
);
?>
scronide
  • 12,012
  • 3
  • 28
  • 33
0

This would do the trick

$search = array('@(<a\s*[^>]*href=[\'"]?)(https?://)?@');
$replace = array('\1proxy2.php?url=');
$new_content = preg_replace($search, $replace, $content);

Result:

<a href="proxy2.php?url=/test/page/">
<a href="proxy2.php?url=google.se/test/">

bisko
  • 3,948
  • 1
  • 27
  • 29
0

Simply make your proxy2.php a little smarter. If a fully qualified URL comes in (http://...), redirect to that. If a local URL comes in (e.g. /test/page/), drop in what's missing (e.g. http://www.mylittleapp.com/test/page/) and redirect.

Derek Illchuk
  • 5,638
  • 1
  • 29
  • 29
  • But that's not really how I want it. I want every URL to go through this script. –  Aug 09 '09 at 20:10
  • If your proxy2.php script receives url=/test/page/, can it not determine the fully qualified URL? – Derek Illchuk Aug 09 '09 at 20:15
  • I do determine which URL it is with a variable named $url. But that's only the http://test.com and not with the maps and etc. –  Aug 09 '09 at 20:17
0

it's me Sara. Scronide, your code did'nt work. It still returns:

<a href="proxy2.php?url=/test/page/">
<a href="proxy2.php?url=google.se/test/">

Instead of what i wanted it to show, i wanted it to show like this, with the url prepended:

<a href="proxy2.php?url=**THEURLHERE.COM**/test/page/">
<a href="proxy2.php?url=google.se/test/">

SORRY, IT DID WORK, I WAS DOING SOMETHING WRONG WITH THE URL VARIABEL. THANK U SCRONIDE!