views:

645

answers:

4

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/"&gt;

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/"&gt;

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.

A: 

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
That didn't seem to work unfortunately. Maybe you misunderstood what I wrote. If http:// is detected i want it to be that way, but if not detected -> then add the variabel $url in front. Did that make things clearer? :p
Well if I run it on the data above, it works flawlessly: <a href="proxy2.php?url=/test/page/"> <a href="google.se/test/">; Of course, if you are trying it on UTF-8 encoded data, please add u flag after the last @.
bisko
the google.se/test/ should read http:// google.se/test/.
bisko
Yes, It behaves that way right now. But i would like http://google.se/test/ to also go through the script like proxy.php?url=http://google.se --- Right now it goes directly to google page.
updated the answer, hope it works now ;)
bisko
A: 

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
But that's not really how I want it. I want every URL to go through this script.
If your proxy2.php script receives url=/test/page/, can it not determine the fully qualified URL?
Derek Illchuk
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.
Sorry, I don't understand.
Derek Illchuk
+1  A: 

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
A: 

Hi, 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!