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 7498521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:30:13+00:00 2026-05-29T19:30:13+00:00

I am looking for help to write an efficient PHP algorithm to help me

  • 0

I am looking for help to write an efficient PHP algorithm to help me find occurances of a String within another string. Here is currently the situation.

I have two arrays. The first array is the array with text that needs searched (haystack). The second array is an array of terms ot find (needle).

I know that my first array has at least one of my terms from the needles. So, the algorithm needs to say ‘is array2[0] found inside array1[0]? if not, loop, is array2[1] found inside array1[0], etc’ If it is found, exit, advance array1[1] pointer and repeat the process.

I want to make sure this is efficient as I have 10s of 1000s of entries to pricess, and my needle array has 1100 individual needles.

  • 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-29T19:30:15+00:00Added an answer on May 29, 2026 at 7:30 pm

    Ok, let’s start with this algorithm, it might not be the fastest but the result is what you want. (Keep loping UNTIL you found the first match)

    <?php
    for ($i = 0; $i < 1000; $i++) {
        $haystack[] = "Lorem ipsum dolor";
        $needle[] = "no match";
    }
    // $haystack = array("Lorem ipsum dolor", "Quisque placerat", "Cras quis porttitor orci");
    //$needle = array("quis", "Lorem");
    $timestamp1 = time() +  microtime();
    foreach ($haystack as $word){
        foreach ($needle as $pattern){
            if(strpos($word, $pattern) === false){
                //Keep looping
            }else{
                //exit inner loop
                print "'".$pattern."' is in '".$word."'<br />";
                break;
            }
        }
    }
    
    $timestamp2 = time() + microtime();
    print "It took me ".($timestamp2 - $timestamp1)." seconds to realize there was no match";
    

    ?>

    //EDIT: I commented the hard coded array, creating it now dynamically an added a timer.
    It takes about 1 second max, if there is no match.

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

Sidebar

Related Questions

LINQ gurus, I am looking for help to write a query... I have a
This might be too opinionated a question, but looking for help! I have been
Hopefully someone here can help me out - basically I have a logging class
I'm looking for something to help me to write C# code that would be
I'm looking to write a custom route that would allow the following http://localhost/blog/tags/foo Currently
I'm looking for some help with PHP File Upload. I'm trying to upload an
I'm looking for an algorithm to help me build 2D patterns based on rules.
   I'm currently looking for a more efficient method, via VBA, to get information transferred
I'm looking for a java library or some help to write my own interpolation
I'm looking for help setting a new default property value for an inherited control

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.