<?PHP
$str = " Monday Tuesday Wednesday Thursday Friday\nSaturday\tSunday";
function a($str) {
return str_word_count($str,1,"!\"#$%&'()*+,./0123456789-:;<=>?@[\]^_`{|}~");
};
function c($str) {
return preg_split("/\s+/",trim($str));
};
function d($string){
return array_filter(explode(' ', implode(' ', array_map('trim', explode("\n", $string)))));
};
function e($str) {
$str = trim($str);
$words = array();
$len = strlen($str);
$word = '';
for ($i = 0; $i < $len; $i++) {
$char = $str[$i];
$ord = ord($char);
// Is a character
if ($ord > 32 && $ord < 127) {
$word .= $char;
// It's some sort of whitespace
} else {
if ($word) {
$words[] = $word;
}
$word = '';
}
}
if ($word) {
$words[] = $word;
}
return $words;
};
function f($str,$return_hash = false) {
$str = trim($str);
// Word characters are any printable char
$words = str_word_count($str,1,"!\"#$%&'()*+,./0123456789-:;<=>?@[\]^_`{|}~");
if ($return_hash) {
$ret = array();
$num = sizeof($words);
// Odd number of elements, can't build a hash
if ($num % 2 == 1) {
return array();
} else {
// Loop over each word and build a key/value hash
for ($i = 0; $i < $num; $i += 2) {
$key = $words[$i];
$value = $words[$i + 1];
$ret[$key] = $value;
}
return $ret;
}
} else {
return $words;
}
};
a($str);
#c($str);
#d($str);
#e($str);
#f($str);
preferences:
39.63 ms | 402 KiB | 5 Q