如果字符串长度超过 50 封电子邮件,则将电子邮件字符串分成几组

发布于 2024-12-01 11:37:20 字数 525 浏览 1 评论 0原文

有没有一种有效的方法可以将包含电子邮件地址列表的字符串分成 50 个组?假设我有一个包含逗号分隔格式的电子邮件的字符串。类似于... [电子邮件受保护][电子邮件受保护],[电子邮件受保护] 等等。

最明显的方法可能是使用数组,但是有没有办法使用字符串函数来做到这一点?我查看了 substr 和 str_split ,它们似乎并没有完全完成这项工作。

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... [email protected],[email protected],[email protected] 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绳情 2024-12-08 11:37:20

搜索第 50 次出现的逗号并在此之后分割字符串应该会更有效。
所以找到这个职位。我认为这个 http://www.php.net/manual/en/ function.strpos.php#102336 应该是一个解决方案。
然后用 substr 分割字符串并删除新字符串 pos 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 = '[email protected],[email protected],[email protected],...';

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

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 = '[email protected],[email protected],[email protected],...';

$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);
}
财迷小姐 2024-12-08 11:37:20

是的,find

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

或者你可以使用

一整套 strn*pos 函数来查找大海捞针中第 n 次出现的针。我更喜欢 strnpos 的这种实现,因为当提供长度为 0 的针时,它不会发出可见的警告(诚然,这是非标准行为)。基于我的版本 [最初发布于 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; 
?>

Yeah, find

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

Or you could use

A complete set of strn*pos functions that look for the nth occurrence of the needle in the haystack. I prefer this implementation of strnpos because it doesn't give visible warnings when supplied with a needle of length 0 (which is, admittedly, non-standard behavior). Based on a version I [originally posted on 05-MAR-2010]; this new version conforms more to the semantics of 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; 
?>
不语却知心 2024-12-08 11:37:20
$emails = '[email protected],[email protected],[email protected],...';
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;
$emails = '[email protected],[email protected],[email protected],...';
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文