如果字符串长度超过50封邮件,则将字符串拆分为组


split string of emails into groups if string is longer than 50 emails

是否有一种有效的方法将包含电子邮件地址列表的字符串分成50个组?假设我有一个字符串,其中包含逗号分隔格式的电子邮件。之类的……email1@host.com,email2@host.com,email3@host.com等。

最明显的方式来做到这一点可能是一个数组,但有没有一种方法来做它与字符串函数?我已经看过substr和str_split,它们似乎并没有完成这项工作。

到目前为止,搜索逗号出现的第50次并在此之后分割字符串应该更有效。找到这个位置。我认为http://www.php.net/manual/en/function.strpos.php#102336应该是一个解决方案。然后使用substr拆分字符串,并删除新字符串第1处的逗号。

缩短php代码以满足您的需求:

<?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);
}

找到

$string="word_1, word_2, word_3"; 
$array=preg_split("/([,]{49}[,])+",$string);

或者你可以用

一组完整的strn*pos函数,用于在干草堆中查找指针的第n次出现。我更喜欢strnpos的这种实现,因为当提供长度为0的指针时,它不会给出可见的警告(诚然,这是一种非标准行为)。基于版本I[最初发布于2010年3月5日];这个新版本更符合strpos的语义。

<?php 
/** 
 *  This function implements all the strn*pos functions, which return the $nth occurrence of $needle 
 *  in $haystack, or false if it doesn't exist / when illegal parameters have been supplied. 
 * 
 *  @param  string  $haystack       the string to search in. 
 *  @param  MIXED   $needle         the string or the ASCII value of the character to search for. 
 *  @param  integer $nth            the number of the occurrence to look for. 
 *  @param  integer $offset         the position in $haystack to start looking for $needle. 
 *  @param  bool    $insensitive    should the function be case insensitive? 
 *  @param  bool    $reverse        should the function work its way backwards in the haystack? 
 *  @return MIXED   integer         either the position of the $nth occurrence of $needle in $haystack, 
 *               or boolean         false if it can't be found. 
 */ 
function strnripos_generic( $haystack, $needle, $nth, $offset, $insensitive, $reverse ) 
{ 
    //  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; 
    } 
    if( $insensitive ) { 
        $haystack = strtolower( $haystack ); 
        $needle   = strtolower( $needle   ); 
    } 
    if( $reverse ) { 
        $haystack = strrev( $haystack ); 
        $needle   = strrev( $needle   ); 
    } 
    //  $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 false === $offset || ! $reverse ? $offset : strlen( $haystack ) - $offset; 
} 
/** 
 *  @see    strnripos_generic 
 */ 
function strnpos( $haystack, $needle, $nth, $offset = 0 ) 
{ 
    return strnripos_generic( $haystack, $needle, $nth, $offset, false, false ); 
} 
/** 
 *  @see    strnripos_generic 
 */ 
function strnipos( $haystack, $needle, $nth, $offset = 0 ) 
{ 
    return strnripos_generic( $haystack, $needle, $nth, $offset, true, false ); 
} 
/** 
 *  @see    strnripos_generic 
 */ 
function strnrpos( $haystack, $needle, $nth, $offset = 0 ) 
{ 
    return strnripos_generic( $haystack, $needle, $nth, $offset, false, true ); 
} 
/** 
 *  @see    strnripos_generic 
 */ 
function strnripos( $haystack, $needle, $nth, $offset = 0 ) 
{ 
    return strnripos_generic( $haystack, $needle, $nth, $offset, true, true ); 
} 
$haystack = 'Dit is een HoTtentotTentenTentenToonstellingTest!'; 
echo strnpos  ( $haystack, 't', 5 ), ' === ', strnpos  ( $haystack, 116, 5 ), PHP_EOL; 
echo strnipos ( $haystack, 't', 5 ), ' === ', strnipos ( $haystack, 116, 5 ), PHP_EOL; 
echo strnrpos ( $haystack, 't', 5 ), ' === ', strnrpos ( $haystack, 116, 5 ), PHP_EOL; 
echo strnripos( $haystack, 't', 5 ), ' === ', strnripos( $haystack, 116, 5 ), PHP_EOL; 
echo PHP_EOL; 
echo strnpos  ( $haystack, 'T', 5 ), ' === ', strnpos  ( $haystack,  84, 5 ), PHP_EOL; 
echo strnipos ( $haystack, 'T', 5 ), ' === ', strnipos ( $haystack,  84, 5 ), PHP_EOL; 
echo strnrpos ( $haystack, 'T', 5 ), ' === ', strnrpos ( $haystack,  84, 5 ), PHP_EOL; 
echo strnripos( $haystack, 'T', 5 ), ' === ', strnripos( $haystack,  84, 5 ), PHP_EOL; 
?>
$emails = 'email1@host.com,email2@host.com,email3@host.com,...';
if( substr_count( $emails, '@' ) > 50 )
{
    $groups = explode( ',', $emails );
    $groups = array_chunk( $groups, 50 );
    $emails = '';
    foreach( $groups as $k => $group )
    {
        $group_data = implode( ', ', $group );
        $emails .= "{$group_data}<hr/>";
    }
}
echo $emails;