Setting URL in html codes to be valid

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yisheng
Forum Newbie
Posts: 8
Joined: Wed Jul 25, 2007 10:34 pm
Location: Singapore

Setting URL in html codes to be valid

Post by yisheng »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I'm trying to do a page which allow user to enter a url, retrieve the content and displaying in my page.
I know i can use iframe to do this, but i would like to use object instead. I found out that object tag (in IE) does not allow data to be from a remote server.

I don't wish to give up and plan to create a page locally by retrieving the data from the input url.
The problem arise for me is the links they had in their page. Such as image src and a href.

I solved these problems by using DOMDocument, retrieving all tags, get attributes for src and href and check if it's valid and change according.
Here is a example of my codes:

Code: Select all

$body = file_get_contents($url);
    $doc = new DOMDocument;
    $html = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8"); 
    @$doc->loadHTML($html);
    $allTags = $doc->getElementsByTagName("*");
    foreach($allTags as $tag)
    {
      if($tag->hasAttribute("href"))
      {
        $value = $tag->getAttribute("href");
        //Validate and change the attribute here
      }

       //Do same thing to attribute src here
    }
    echo $doc->saveHTML();
Ok, now is the real headache part for me which i'm losing hair over. CSS, i can't find a way to get the url("../image/something.gif") and change according to link. Is there a way to get those url so i could validate and insert "http://somepage.com" to replace the ".."?

From the above codes, is it a good way and is there a better way to do this?

Thanks in advance.

PS. I just start coding PHP for around 4 to 6 months on and off. Please use simple explaination if possible.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Take a look at this page. It works perfectly well in IE and FF.

http://www.dynamicdrive.com/dynamicinde ... nview2.htm
yisheng
Forum Newbie
Posts: 8
Joined: Wed Jul 25, 2007 10:34 pm
Location: Singapore

Post by yisheng »

Thanks for your reply.

I've refer to your link and saw it using iFrame to link to the external page. :(
I with to achieve this by using the object tag.

Using

Code: Select all

<object standby="Loading..." data="http://www.somepage.com" type="text/html" width="600" height="400" style="background-color:#fff;">
  alt : <a href="http://www.somepage.com">http://www.somepage.com</a>
</object>
But it's restriction is not able to link to external page ( in IE ). Therefore i create a script ( in my first post ) to extract the content and have the object tag refer to my script.
yisheng
Forum Newbie
Posts: 8
Joined: Wed Jul 25, 2007 10:34 pm
Location: Singapore

Post by yisheng »

Bring.Up.My.Post

Anyone with ideas on how it can be done ( first post )??

Thanks in advance.
Post Reply