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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:04:43+00:00 2026-05-25T00:04:43+00:00

Is there an efficient way to split a string containing a list of email

  • 0

Is there an efficient way to split a string containing a list of email addresses into groups of say 50? Say I have a string that contains emails in a comma-separated format. Something like… email1@host.com,email2@host.com,email3@host.com and so on.

The most obvious way to do this would probably be an array, but is there a way to do it with string functions? I have looked at substr and str_split and they don’t quite seem to do the job.

  • 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-25T00:04:44+00:00Added an answer on May 25, 2026 at 12:04 am

    It should be by far more efficient to search for the 50th occurrence of a comma and split the string after this.
    So find this Position. I think this http://www.php.net/manual/en/function.strpos.php#102336 should be a solution for that.
    Then split the string with substr and remove the comma at pos 1 of the new string.

    Shortened PHP-Code to fit your needs:

    <?php 
    
    function strnpos( $haystack, $needle, $nth, $offset = 0 ) { 
        //  If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. 
        if(!is_string($needle)) 
            $needle = chr((int)$needle ); 
    
        //  Are the supplied values valid / reasonable? 
        $len = strlen( $needle ); 
        if(1 > $nth || 0 === $len) 
            return false; 
    
        //  $offset is incremented in the call to strpos, so make sure that the first 
        //  call starts at the right position by initially decreasing $offset by $len. 
        $offset -= $len; 
        do { 
            $offset = strpos( $haystack, $needle, $offset + $len ); 
        } while( --$nth  && false !== $offset ); 
    
        return $offset;
    } 
    
    $emails_str = 'email1@host.com,email2@host.com,email3@host.com,...';
    
    $pos = strnpos($emails_str, ',', 50, 0);
    while($pos) {
        // Do sth. with the group...
        echo substr($emails_str, 0, $pos), PHP_EOL; 
    
        // Cut this part out of the string
        $emails_str = substr($emails_str, $pos+1);
        $pos = strnpos($emails_str, ',', 50, 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There must be a fast and efficient way to split a (text) string at
What is the most efficient way to split a string by a very simple
Is there a more efficient way to create a string array from Guava's Splitter
What is the shortest and most efficient way to decompose a string array into
Is there an efficient way of detecting if a jpeg file is corrupted? Background
Is there an efficient way to tell if a DOM element (in an HTML
Is there an efficient way to store the compiled regexes (compiled via regcomp(), PCRE)
Is there an efficient way to create a file with a given size in
Is there an efficient way to take a subset of a C# array and
Is there an efficient way to version store procedures written in PL/SQL? (I only

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.