不再有分裂包含電子郵件地址列表入組串說50說,我有一個包含以逗號分隔格式的郵件串的有效途徑。喜歡的東西... EMAIL1 @ host.com,EMAIL2 @ host.com,EMAIL3 @ host.com等。電子郵件成組的分割字符串,如果字符串的長度超過50個電子郵件
最明顯的方法,這樣做很可能是一個數組,但有沒有辦法用字符串函數做呢?我看過substr和str_split,他們似乎沒有退出這個工作。
感謝
不再有分裂包含電子郵件地址列表入組串說50說,我有一個包含以逗號分隔格式的郵件串的有效途徑。喜歡的東西... EMAIL1 @ host.com,EMAIL2 @ host.com,EMAIL3 @ host.com等。電子郵件成組的分割字符串,如果字符串的長度超過50個電子郵件
最明顯的方法,這樣做很可能是一個數組,但有沒有辦法用字符串函數做呢?我看過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 = '[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);
}
呀,找
$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 = '[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;