Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Apr 2009
    Posts
    135
    Thanks
    83
    Thanked 0 Times in 0 Posts

    strpos() Isn't Returning Accurate Results???

    I have created a script that uses CURL to retrieve a webpage where it searches to see if a link exists.

    When this search takes place it also completely rips the page at the moment so I could find the problem as I knew that something was up with it.

    I imported 1000 URL's to rip and scan for a link and then ran 3 scans on the full list.

    Scan 1 returned 317/1000 ok.

    Scan 2 returned 312/1000 ok.

    Scan 3 returned 319/1000 ok.

    I have looked at the page rips to see the differences in the URL's that it was saying did not have the link in place and they were there. In exactly the same format as the previous searches.

    So in 3 searches of the same webpage only once did it return ok on the strpos match when all 3 times it was scanning the same page.

    Is this a problem with CURL not grabbing the page fast enough?

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    How about some example code so we can see/try what you're talking about?

  • Users who have thanked Inigoesdr for this post:

    Sussex_Chris (02-27-2010)

  • #3
    Regular Coder
    Join Date
    Apr 2009
    Posts
    135
    Thanks
    83
    Thanked 0 Times in 0 Posts
    This is the script I am using, with all DB side of it removed for easy reading:

    PHP Code:
    <?php
    function Return_Content_From_URL($url){
        
    $userAgent "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729)";
        
    $ch curl_init();
        
    curl_setopt($chCURLOPT_USERAGENT$userAgent);
        
    curl_setopt($chCURLOPT_URL,$url);
        
    curl_setopt($chCURLOPT_FAILONERRORtrue);
        
    curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
        
    curl_setopt($chCURLOPT_AUTOREFERERtrue);
        
    curl_setopt($chCURLOPT_RETURNTRANSFER,true);
        
    curl_setopt($chCURLOPT_VERBOSEfalse);
        
    $htmlcurl_exec($ch);
        
    $newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
        
    $content str_replace($newlines""html_entity_decode($html));
        return 
    $content;
        }

    $url "http://urltosearch.com/";
    $result get_search_results($url);

    $pos strpos($result"keyword");
    if(
    $pos==false){echo "Keyword Not Found In URL: $url<br />";}
    ?>

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    From the strpos page:

    This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
    http://uk2.php.net/manual/en/function.strpos.php

    Code:
    if (strpos($result, "keyword") !== false)
    {
        echo "Keyword Not Found In URL: $url<br />";
    }

  • Users who have thanked MattF for this post:

    Sussex_Chris (02-27-2010)

  • #5
    Regular Coder
    Join Date
    Apr 2009
    Posts
    135
    Thanks
    83
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    From the strpos page:



    http://uk2.php.net/manual/en/function.strpos.php

    Code:
    if (strpos($result, "keyword") !== false)
    {
        echo "Keyword Not Found In URL: $url<br />";
    }
    This seems to be returning true whether the value be there or not. I think this works ok:
    PHP Code:
    if (strpos($result"keyword") === false

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Sussex_Chris View Post
    PHP Code:
    if (strpos($result"keyword") === false
    Yup. That's the one you should be using. A slight error on my part there.

  • Users who have thanked MattF for this post:

    Sussex_Chris (02-27-2010)


  •  

    Posting Permissions

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