www.webdeveloper.com
Results 1 to 11 of 11

Thread: Put HTML from another page into a string

  1. #1
    Join Date
    Mar 2005
    Posts
    159

    Put HTML from another page into a string

    Hi,
    I want to be able to put the HTML of another page into a string. What is the best way to do this? I couldn't really thing of a better way to say this so if you don't understand just tell me and I'll try harder.
    Thanks.

  2. #2
    Join Date
    Sep 2005
    Location
    Portland Oregon
    Posts
    153
    What I do is:

    $dat = '
    Your
    Html
    Code
    Goes
    Here
    ';

    Only cavaet is to ensure that any single quotes " ' " are escaped (\'). Hence use normal quotes ' " ' for all strings where possible. Then you can printf$dat) or do whatever you want with it.

    I've found using normal quotes in all instances where a variable name is not used is best practices for me. Then the above works with no sweat.

    tony

  3. #3
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,972
    I think this is what you're getting at.
    PHP Code:
    $filename 'something.htm';
    $file fopen($filename,'r');
    $page fread($file,filesize($filename));

    echo 
    $page

  4. #4
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    to you want to read or phrase the html? That will phrase it, this will read it in a n editalbe text box.
    PHP Code:
    <?php 

            
    if(empty($_GET['action'])){ //opens file to be edited
    ?> 

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=write" method="post">
    <textarea name="contents" rows="10" cols="60"> 
    <?php 
    //this part will open the html page into a text box
    //if you mae changes to it you can click the submit buton to save changes
     
    $fp=fopen("page_to_read.html""r"); //this is the html pae you want to read. same as if you use the next section.

     
    while (!feof($fp)){ 
     
    $line=fgets($fp); 
     print 
    stripslashes("$line"); 
     }
    //end while 

     
    fclose($fp); 

     
    ?> 
     </textarea><br/><br />
     
    <input type="submit" value="Submit" /> 
    </form> 

    <?php 
    //if you are gong to save the chnages you need to modify the permissions of that page to chmod 777
    }else{  //saves changes to the file selected if you make chages

    $fp=fopen("page_to_read.html""w"); //same file as above
     
    fputs($fp$_REQUEST['contents']); 


     if (
    fclose($fp)){ 
     print 
    "<h2>File successfully saved</h2>\n"
     }else{ 
     print 
    "<h2><font style=\"color:red;\">There was a problem saving the file</font></h2>\n";
     } 



    }
    //end if action 


     
    ?>

  5. #5
    Join Date
    Mar 2005
    Posts
    159
    I want to do what the tree said but that doesn't work with pages that are not on my web space.
    THanks

  6. #6
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    The tree's method can be simplified to simply:
    PHP Code:
    $string file_get_contents('http://www.domain.com/file.htm'); 
    If that doesn't work run the following to see if url file opening is allowed on your server:
    PHP Code:
    <?php

    print ini_get('allow_url_fopen');

    ?>
    If url file open is not allowed on your server I have a function to get round that.

  7. #7
    Join Date
    Mar 2005
    Posts
    159
    Hmmm... I get this error:
    Warning: file_get_contents(http://www.yell.com/ucs/UcsSearchAct...&location=DE23 6HB&scrambleSeed=32581&searchType=classic&startOoaAt=10&ooascrambleSeed=32581&ooa=on): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/sftr/public_html/takeaway/index.php on line 7
    And the ini function returns 1.
    What's up with it?
    Thanks.

  8. #8
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Take the line break out of the URI and put single quotes around it.
    file_get_contents('http://www.domain.com/file.ext?string=123');

  9. #9
    Join Date
    Mar 2005
    Posts
    159
    That is exactly how it is

    $string = file_get_contents('http://www.yell.com/ucs/UcsSearchAction.do?startAt=0&keywords=takeaways&location='.$_POST['postcode'].'&scrambleSeed=32581&searchType=classic&startOoaAt=10&ooascrambleSeed=32581&ooa=on');
    echo $string;

  10. #10
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Your code works for me so it must be a server error.

    Try this:
    PHP Code:
    <?php
    $url 
    'http://www.yell.com/ucs/UcsSearchAction.do?startAt=0&keywords=takeaways&location='.$_POST['postcode'].'&scrambleSeed=32581&searchType=classic&startOoaAt=10&ooascrambleSeed=32581&ooa=on';
    $string http_retrieve($url);
    if(!isset(
    $string['errorstring'])){
       echo 
    $string['body'];
    }else{
       echo 
    'could not open file';
    }

    function 
    http_retrieve($url$followRedirects true)
    {
       
    # Returns array(['url'] array(['headers']) ['body']) on success
       # Returns array(['url'] ['errornumber'] ['errorstring']) on failure
       
    $url preg_replace('/[\r]|[\n]/'''$url);     
       
    $url_parsed parse_url($url);
       if (empty(
    $url_parsed['scheme'])) $url_parsed parse_url('http://'.$url);
       
    $return['url'] = $url_parsed;
       if(!isset(
    $url_parsed["port"])) $url_parsed["port"] = 80;
       
    $return['url']['port'] = $url_parsed["port"];
       
    $path $url_parsed["path"];
       if(empty(
    $path)) $path="/";
       if(!empty(
    $url_parsed["query"])) $path .= "?".$url_parsed["query"];
       
    $return['url']['path'] = $path;
       
    $host $url_parsed["host"];
       
    $foundBody false;
       
    $out "GET $path HTTP/1.0\r\n";
       
    $out .= "Host: $host\r\n";
       
    $out .= "Connection: Close\r\n\r\n";
       if(!
    $fp = @fsockopen($host$url_parsed["port"], $errornumber$errorstring5)){
           
    $return['errornumber'] = $errornumber;
           
    $return['errorstring'] = $errorstring;
           return 
    $return;
       }
       
    fwrite($fp$out);
       
    $headers NULL;
       
    $body NULL;
       while (!
    feof($fp)) {
           
    $s fgets($fp128);
           if (
    $s == "\r\n"){
               
    $foundBody true;
               continue;
           }
           if (
    $foundBody){
               
    $body .= $s;
           }else{
               if((
    $followRedirects) && (stristr($s"location:") != false)) 
                       return 
    http_retrieve(trim(preg_replace("/location:/i"""$s)));
               
    $headers .= $s;
           }
       }
       
    fclose($fp);
       
    $headers explode("\n"trim($headers));
       foreach(
    $headers as $header){
           if(
    strpos($header':')){
               list(
    $header$value) = explode(':'$header);
               
    $return['headers'][trim($header)] = trim($value);
           }else{
               
    $return['headers'][substr($header04)] = $header;
           }
       }
       
    $return['body'] = trim($body);
       return 
    $return;
    }
    ?>
    Last edited by bokeh; 09-28-2005 at 02:50 PM.

  11. #11
    Join Date
    Mar 2005
    Posts
    159

    So then...

    I put that code there and put the function before the actual code so that it actually worked... but now i get this:

    Bad Request
    Your browser sent a request that this server could not understand.

    The request line contained invalid characters following the protocol string.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles