Page 1 of 1

Setting URL in html codes to be valid

Posted: Wed Jul 25, 2007 10:55 pm
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]

Posted: Thu Jul 26, 2007 3:53 am
by aceconcepts
Take a look at this page. It works perfectly well in IE and FF.

http://www.dynamicdrive.com/dynamicinde ... nview2.htm

Posted: Thu Jul 26, 2007 4:18 am
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.

Posted: Sun Jul 29, 2007 8:12 pm
by yisheng
Bring.Up.My.Post

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

Thanks in advance.