hellonoko Posted March 19, 2009 Share Posted March 19, 2009 I am using strpos() to compare URLS. However in my function it doesn't seem to return anything. When I copy the bit of code out into its own page or outside of my function it works. Any ideas? Code is on line 74. Thanks. <?php //error_reporting(E_ALL); //echo $site_url = 'http://www.empreintes-digitales.fr/'; $target_url = "http://www.empreintes-digitales.fr"; //$target_url = 'http://redthreat.wordpress.com/'; //$target_url= 'http://www.kissatlanta.com/blog/'; //$target_url= 'http://www.empreintes-digitales.fr/'; $url = ""; $link = ""; $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)'; crawl_page( $target_url, $userAgent); function crawl_page( $target_url, $userAgent) { $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); curl_setopt($ch, CURLOPT_URL,$target_url); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $html = curl_exec($ch); if (!$html) { echo "<br />cURL error number:" .curl_errno($ch); echo "<br />cURL error:" . curl_error($ch); exit; } // // load scrapped data into the DOM // $dom = new DOMDocument(); @$dom->loadHTML($html); // // get only LINKS from the DOM with XPath // $xpath = new DOMXPath($dom); $hrefs = $xpath->evaluate("/html/body//a"); // // go through all the links and store to db or whatever // for ($i = 0; $i < $hrefs->length; $i++) { $href = $hrefs->item($i); $url = $href->getAttribute('href'); $links_1[$link] = $url; //echo $absolute_links[$link] = relative2absolute($target_url, $url); //echo '<br>'; //if the $url does not contain the web site base address: http://www.thesite.com/ then add it onto the front echo gettype($url); echo gettype($target_url); echo '<b>'; echo $pos = strpos($url , $target_url); echo '</b>'; if ( $pos == FALSE ) { echo 'INCOMPLETE: '.$url; echo '<br>'; } else { echo 'COMPLETE: '.$url; echo '<br>'; } } } Link to comment https://forums.phpfreaks.com/topic/150087-strpos-returning-empty/ Share on other sites More sharing options...
markjoe Posted March 19, 2009 Share Posted March 19, 2009 When checking for false from, strpos() use === since strpos() can return (int) 0. 0 == false but 0 !== false === checks value and type, == only checks value Link to comment https://forums.phpfreaks.com/topic/150087-strpos-returning-empty/#findComment-788221 Share on other sites More sharing options...
hellonoko Posted March 19, 2009 Author Share Posted March 19, 2009 I have tried that but its not so much that as... On line 74 where I echo $pos = strpos(); It echos nothing. The function returns empty? Link to comment https://forums.phpfreaks.com/topic/150087-strpos-returning-empty/#findComment-788224 Share on other sites More sharing options...
markjoe Posted March 19, 2009 Share Posted March 19, 2009 if the case returns FALSE, then yes, it returns 'empty'. <?php echo "{".strpos('abc', 'a')."}<br>"; echo "{".strpos('abc', 'b')."}<br>"; echo "{".strpos('abc', 'c')."}<br>"; echo "{".strpos('abc', 'd')."}<br>"; ?> that code outputs {0} {1} {2} {} I think this explains it pretty well: http://us2.php.net/manual/en/function.strpos.php Link to comment https://forums.phpfreaks.com/topic/150087-strpos-returning-empty/#findComment-788231 Share on other sites More sharing options...
hellonoko Posted March 19, 2009 Author Share Posted March 19, 2009 I understand that but no matter how I change it around it echos nothing. But if i take that section of code and put it in its own page. It works fine. Just not inside the function. Link to comment https://forums.phpfreaks.com/topic/150087-strpos-returning-empty/#findComment-788249 Share on other sites More sharing options...
hellonoko Posted March 19, 2009 Author Share Posted March 19, 2009 To be more specific: function checkURL($url, $target_url) { echo $url.'<br>'; echo $target_url.'<br>'; echo gettype($url).'<br>'; echo gettype($target_url).'<br>'; echo '<b>'; echo $pos = strpos($url , $target_url); echo '</b>'; } Returns: http://empreintes-digitales.fr/board/register.php http://www.empreintes-digitales.fr string string http://empreintes-digitales.fr/board/login.php?action=forget http://www.empreintes-digitales.fr string string # http://www.empreintes-digitales.fr string string http://66.102.9.104/translate_c?hl=fr&sl=fr&tl=en&u=www.empreintes-digitales.fr/index.php http://www.empreintes-digitales.fr string string And on and on. Nothing from $pos = strpos() Link to comment https://forums.phpfreaks.com/topic/150087-strpos-returning-empty/#findComment-788251 Share on other sites More sharing options...
markjoe Posted March 19, 2009 Share Posted March 19, 2009 every one of those is false. based on the examples of $url and $target_url you just posted, strpos() will return boolean false for each of them, boolean false does not translate to a string, so nothing is printed. Link to comment https://forums.phpfreaks.com/topic/150087-strpos-returning-empty/#findComment-788253 Share on other sites More sharing options...
hellonoko Posted March 19, 2009 Author Share Posted March 19, 2009 I finally got it to work but not how it should. If i use strpos( $url , "http" ); it works However if I use: strpos ($url, $target_url); its always false like you said. because its not comparing correctly. Any ideas on that one? Link to comment https://forums.phpfreaks.com/topic/150087-strpos-returning-empty/#findComment-788254 Share on other sites More sharing options...
markjoe Posted March 19, 2009 Share Posted March 19, 2009 actually it is comparing correctly. "http://empreintes-digitales.fr/board/register.php" simply does not contain the string: "http://www.empreintes-digitales.fr" perhaps you want to shorten your target_url a bit? I don't know what the purpose of this is, but you could strip the target url down to 1st and 2nd level domains and get far better matches. since "http://empreintes-digitales.fr/board/register.php" does contain "empreintes-digitales.fr" Link to comment https://forums.phpfreaks.com/topic/150087-strpos-returning-empty/#findComment-788256 Share on other sites More sharing options...
hellonoko Posted March 19, 2009 Author Share Posted March 19, 2009 Arhmmm... Thanks. I might need to stop looking at this for a day or something I think I got it working how I want now thanks for your help. Link to comment https://forums.phpfreaks.com/topic/150087-strpos-returning-empty/#findComment-788257 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.