Replacing all urls?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Rectangle
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Dec 08, 2012 1:24 pm

Replacing all urls?

Postby Rectangle » Sat Dec 08, 2012 1:44 pm

I have a web site where the user can navigate to an offsite URL, but I need to apply "http://mysite.com/?url=" to the start of every URL in the target page's source (images, links, forms, etc). I'm assuming the best way to do this is using regular expressions, but I haven't the slightest idea as to what pattern I should use.

The alternative would be to use the standard string manipulation functions to do the trick, but I'd imagine that to be several more lines of code and hold a longer execution time. Any hints?
Last edited by Rectangle on Sun Dec 09, 2012 2:06 pm, edited 1 time in total.

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Re: Replacing all urls?

Postby simplypixie » Sun Dec 09, 2012 5:05 am

Unless I am missing something here, I am not sure why you are trying to complicate matters. In your code you just need

Code: Select all

<a href="http://mysite.com/?url=<?php echo $url; ?>">Link Text</a>

Rectangle
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Dec 08, 2012 1:24 pm

Re: Replacing all urls?

Postby Rectangle » Sun Dec 09, 2012 1:46 pm

That is correct. However, it needs to be applied to source code from offsite HTML code, not just my own. I have edited my first post to be more specific about this.
So would it be better to use the DOMDocument class or standard string manipulation functions? Or is there a way to do this in just a few lines of code using RegEx?

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Re: Replacing all urls?

Postby simplypixie » Mon Dec 10, 2012 6:23 am

I am really not sure what you are trying to do - you cannot amend the HTML source of external websites.

Rectangle
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Dec 08, 2012 1:24 pm

Re: Replacing all urls?

Postby Rectangle » Mon Dec 10, 2012 7:29 am

simplypixie wrote:I am really not sure what you are trying to do - you cannot amend the HTML source of external websites.

Of course you can! There are web proxies/anonymizers all over the internet that already do this. They get the source of an external page (via fopen, file_get_contents, fsockopen, httprequest, domdocument or otherwise), create a local cache, modify it's contents to work on their site, redirect everything back to the same page, and then display the modified page. There's even a few well-known open source projects (such as PHProxy) that do something like this.

My issue is that most of these well-known proxy sites and projects have been blacklisted by OpenDNS on my current network configuration, so I am forced to write my own specification of which OpenDNS is not familiar with. And thus far, I have successfully written up to the point where a user can navigate to another site from within my own site... But in order for navigation to continue any further than a single page, the URLs contained in that page must be modified accordingly.

That being said, I've decided to just use DOMDocument to parse and replace html elements. Thanks anyhow.

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Re: Replacing all urls?

Postby simplypixie » Mon Dec 10, 2012 8:36 am

So basically you are saying you want to hack someone else's website and alter their source code - that is why
most of these well-known proxy sites and projects have been blacklisted by OpenDNS

Rectangle
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Dec 08, 2012 1:24 pm

Re: Replacing all urls?

Postby Rectangle » Mon Dec 10, 2012 11:43 am

Are you serious?? Did you even research what OpenDNS is? How much coding do you actually know, anyhow? No. There is no 'hacking' involved. PHP is a server-side scripting language built for dynamic server/client networking, and more than half of it's functions revolve around outbound access (most SSL-based shops access other networks on various ports, for example). Furthermore, even without PHP, whenever you visit a site you are freely accessing their HTML code (how do you think a browser knows what to render??).

And for f#^$ sake, google "anonymous proxy" and see how many people are doing this, you moron.
You are calling > 3 million companies malicious 'hackers' when all they are doing is trying to protect your identity while browsing the web or trying to allow access to sites like google or youtube to that would normally be blocked on your network at school or work.

This has gone far off-subject, now. I merely asked a simple string manipulation question (isn't that what programming forums are for?), and instead of getting answers I decided to settle for the DOMDocument class and then was rudely treated like a 'hacker' as if I'm some 12 year old kid who's parents are blocking porn sites from him. I just want to use google at work when it's time for research. Thanks for the warm invite.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot], diipkdotnet and 1 guest