如果字符串长度超过 50 封电子邮件,则将电子邮件字符串分成几组
有没有一种有效的方法可以将包含电子邮件地址列表的字符串分成 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
搜索第 50 次出现的逗号并在此之后分割字符串应该会更有效。
所以找到这个职位。我认为这个 http://www.php.net/manual/en/ function.strpos.php#102336 应该是一个解决方案。
然后用 substr 分割字符串并删除新字符串 pos 1 处的逗号。
缩短的 PHP 代码以满足您的需求:
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:
是的,find
或者你可以使用
一整套 strn*pos 函数来查找大海捞针中第 n 次出现的针。我更喜欢 strnpos 的这种实现,因为当提供长度为 0 的针时,它不会发出可见的警告(诚然,这是非标准行为)。基于我的版本 [最初发布于 2010 年 3 月 5 日];这个新版本更符合strpos的语义。
Yeah, find
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.