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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:38:28+00:00 2026-05-28T04:38:28+00:00

I’m wondering if it is possible to truncate an array by using a regular

  • 0

I’m wondering if it is possible to truncate an array by using a regular expression.

In particular I have an array like this one:

$array = array("AaBa","AaBb","AaBc","AaCa","AaCb","AaCc","AaDa"...);

I have this string:

$str = "AC";

I’d like the slice of $array from the start to the last occurrence of a string matching /A.C./ (in the sample, “AaCc” at index 5):

$result = array("AaBa","AaBb","AaBc","AaCa","AaCb","AaCc");

How can I do this? I thought I might use array_slice, but I don’t know how to use a RegEx with it.

  • 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-28T04:38:28+00:00Added an answer on May 28, 2026 at 4:38 am

    Here’s my bid

    function split_by_contents($ary, $pattern){
      if (!is_array($ary)) return FALSE; // brief error checking
    
      // keep track of the last position we had a match, and the current
      // position we're searching
      $last = -1; $c = 0;
    
      // iterate over the array
      foreach ($ary as $k => $v){
        // check for a pattern match
        if (preg_match($pattern, $v)){
          // we found a match, record it
          $last = $c;
        }
        // increment place holder
        $c++;
      }
    
      // if we found a match, return up until the last match
      // if we didn't find one, return what was passed in
      return $last != -1 ? array_slice($ary, 0, $last + 1) : $ary;
    }
    

    Update

    My original answer has a $limit argument that served no purpose. I did originally have a different direction I was going to go with the solution, but decided to keep it simple. However, below is the version that implements that $limit. So…

    function split_by_contents($ary, $pattern, $limit = 0){
      // really simple error checking
      if (!is_array($ary)) return FALSE;
    
      // track the location of the last match, the index of the
      // element we're on, and how many matches we have found
      $last = -1; $c = 0; $matches = 0;
    
      // iterate over all items (use foreach to keep key integrity)
      foreach ($ary as $k => $v){
    
        // text for a pattern match
        if (preg_match($pattern, $v)){
    
          // record the last position of a match
          $last = $c;
    
          // if there is a specified limit, capture up until
          // $limit number of matches, then exit the loop
          // and return what we have
          if ($limit > 0 && ++$matches == $limit){
            break;
          }
        }
    
        // increment position counter
        $c++;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I am reading a book about Javascript and jQuery and using one of the
I'm making a simple page using Google Maps API 3. My first. One marker

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.