How do I get first 200 characters

7 replies
I need to check for a string on an external website

Sorry, no results were found

Using get file contents bogs the script down to unusability.

Is there a way to stop getting the file contents from an external webpage after the first 200 characters so I can check to see if the string exists without having to grab the entire webpage contents first?

Rick
#200 #characters
Avatar of Unregistered
  • Profile picture of the author Eric Michalsen
    quick and dirty
    It's weird how I can't put $page and $first in the code block, so I put PAGE AND FIRST


    ... AND, of course I provided this with reading the entire request first! Doh!
    Let me re-evaluate and get back. I think I know how to do this, but need to run a test first.

    reworked:
    PHP Code:
    // Using curl from the command line, we can limit the byte range with the -r switch:
    curl -r 0-20 https://ericmichalsen.com

    // OR, in a PHP file

    URL 'https://ericmichalsen.com';

    CH curl_init();
      
    curl_setopt(CHCURLOPT_URLURL);
      
    curl_setopt(CHCURLOPT_RANGE'0-20');
      
    curl_setopt(CHCURLOPT_BINARYTRANSFER1);
      
    curl_setopt(CHCURLOPT_RETURNTRANSFER1);
    RESULT curl_exec();
    curl_close(CH);
    echo 
    RESULT
    So, I am having success with pulling only so many bytes from an HTML site, but as soon as I attempt to pull a large XML file, it doesn't work.

    Please let me know your use case so I can be of further help


    PHP Code:

    // Get the contents of a page and put into a variable
    PAGE file_get_contents('https://ericmichalsen.com/');

    // Return a part of the string. In this case starting at zero to the 20th character.
    // Obviously change the 20 to what you l ike
    FIRST =  substr(PAGE020);

    // echo the first 20 character
    echo FIRST;

    // or, if you like, run past some logic
    if (preg_match('/SOMETHING/'FIRST)) {
      echo 
    "Found!";
    }
     else {
      echo 
    "Not Found!";

    {{ DiscussionBoard.errors[11197207].message }}
  • Profile picture of the author hometutor
    I just tried using the single line get file php command and it seems the site may be protected.

    I got this error trying to execute your script

    Code:
    Warning: curl_exec() expects exactly 1 parameter, 0 given in /foo on line 89
    results are
    Thanks

    Rick
    {{ DiscussionBoard.errors[11202272].message }}
  • Profile picture of the author Eric Michalsen
    can you private msg me the url you attempting to hit?
    {{ DiscussionBoard.errors[11202324].message }}
  • Profile picture of the author Eric Michalsen
    also, what os are you running the curl command from?
    {{ DiscussionBoard.errors[11202897].message }}
  • Profile picture of the author Eric Michalsen
    So I tried curl on the url you pm'd me. It looks as if the server does not have the range feature enabled, so if I do curl -r 0-199 <URL> I am getting the entire document, and not just the first 200 characters.

    As to the curl error you received, I'm not sure how you have it setup. I am assuming your Apache web server is running in a linux environment?
    {{ DiscussionBoard.errors[11204586].message }}
Avatar of Unregistered

Trending Topics