Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6549115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:03:11+00:00 2026-05-25T12:03:11+00:00

I have a PHP script that looks for links on a page that it

  • 0

I have a PHP script that looks for links on a page that it downloads with CURL_MULTI functions. The downloading is fine and I get the data, but my script randomly crashes when I encounter a page that has the url listed as a nonlink. This is the code:

$fishnof = strpos($nofresult, $supshorturl, 0);
$return[0] = ''; $return[1] = ''; // always good to cleanset

// Make sure we grabbed a link instead of a text url(no href)
if ($fishnof !== false) {
    $linkcheck = rev_strpos($nofresult,'href',$fishnof);
    $endthis = false;
    while($endthis !== true) {
        if($linkcheck > ($fishnof - 25)){ // 19 accounts for href="https://blog. 25 just in case
            $endthis = true;
            break;
        }
        $lastfishnof = $fishnof;
        $fishnof = strpos($nofresult,$supshorturl,$fishnof+1);
        if($fishnof === false){$fishnof = $lastfishnof;$linkcheck = rev_strpos($nofresult,'href',$fishnof);$endthis = true;break;}// This is the last occurance of our URL on this page
        if($linkcheck > $fishnof){$linkcheck = rev_strpos($nofresult,'href',$fishnof);$endthis = true;break;} // We went around past the end of the string(probably don't need this)      
        $linkcheck = rev_strpos($nofresult,'href',$fishnof);
    }
    if($linkcheck < ($fishnof - 25)){ // 19 accounts for href="https://blog. 25 just in case
        $return[0] = 'Non-link.';
        $return[1] = '-';
        $nofresult = NULL; // Clean up our memory
        unset($nofresult); // Clean up our memory
        return $return;
    }
}

This is the custom rev_strpos, which just does a reverse strpos():

// Does a reverse stripos()
function rev_strpos(&$haystack, $needle, $foffset = 0){
    $length = strlen($haystack);
    $offset = $length - $foffset - 1;
    $pos = strpos(strrev($haystack), strrev($needle), $offset);
    return ($pos === false)?false:( $length - $pos - strlen($needle) );
}

so if:

$nofresult = '
Some text.Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
google.com Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
<a href="http://www.google.com">Google</a> Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.';

and

$supshorturl = "google.com";

This should find the position of the second occurance of google.com, where it is inside of a HTML href tag. The problem is that it does not report any error before the crash, my error settings:

ini_set("display_errors", 1);
error_reporting(E_ALL & ~E_NOTICE);
set_error_handler('handle_errors');

My handle_errors() function logs all errors in a file. However no errors are reported before the script crashes. Also my curl_multi processes many URLs, and sometimes it will crash on a certain URL and and other times it crashes on another URL… I am ready to pull out my hair because this seems like such an easy deal… but here I am. Another point of notice is if I remove the while loop then no crash, also if the page has the url in a href tag first then it doesn’t crash. Please help me figure this thing out. Thanks a million!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:03:12+00:00Added an answer on May 25, 2026 at 12:03 pm

    I think you’re making it harder than it needs to be. If rev_strpos is only needed to return the last instance of your search string, and if you aren’t worried about case, use strripos instead.

    From the PHP docs…

    strripos — Find position of last occurrence of a case-insensitive string in a string

    Description

    int strripos ( string $haystack , string $needle [, int $offset = 0 ] )
    

    Find position of last occurrence of a string in a string. Unlike strrpos(), strripos() is case-insensitive.

    If you need it to be case-sensitive, or just want to use your own function for some reason, the problem is in how you are calculating the offset. Specifically in these 2 lines:

    $offset = $length - $foffset - 1;
    $pos = strpos(strrev($haystack), strrev($needle), $offset);
    

    Using your sample “Some text…” and searching for “google.com”, if we don’t specify an offset it calculates the offset as length (500 chars) – offset (0 chars) – 1. Then you use strpos on a 500-char length string starting at offset character 499. You’re never going to find anything that way.

    Since you are reversing your haystack and also your needle, you need to “reverse” your offset. Change the line to:

    $pos = strpos(strrev($haystack), strrev($needle), $length - $offset);
    

    (Actually, you should change your prior line to calculate the $offset where you want it to be, but you get the point…)

    UPDATE:

    Further to the recommendations about using Regex, it’s really trivial to get locations:

    function getOffsets( $url, $baseRegex, $text ){
        $results = array();
        $regex= str_replace( '%URL%', $url, $baseRegex );
        preg_match_all( $regex, $text, $matches, PREG_OFFSET_CAPTURE );
    
        foreach ( $matches[0] as $match )
            array_push( $results, ($match[1] + strpos( $match[0], $url )) );
    
        return $results;
    }
    
    $linkRegex = '/<a[^>]*href="[^"]*%URL%[^"]*"[^>]*>/i';
    $linkLocations = getOffsets( $url, $linkRegex, $text );
    //Array
    //(
    //    [0] => 395
    //)
    
    $anyRegex = '/%URL%/i';
    $allLocations = getOffsets( $url, $anyRegex, $text );
    $nonlinkLocations = array_diff( $allLocations, $linkLocations );  //all non-links
    //Array
    //(
    //    [0] => 188
    //)
    

    This really should be preferable to the rev_strpos & while loop gimmicks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that initialises an image gallery. It loops through all
I have a php script that runs a mysql query, then loops the result,
I have php script that creates a temporary watermark image for users that are
I have a PHP script that runs as a CGI program and the HTTP
I have a PHP script that needs to determine if it's been executed via
I have a PHP script that can encode a PNG image to a Base64
I have a PHP script that sends critical e-mails. I know how to check
I have a PHP script that processes file uploads. The script tries to organise
I have a PHP script that pushes the headers to allow a file to
I have a PHP script that executes a .bat file using system(cmd /c C:\dir\file.bat);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.