Skip to content

Instantly share code, notes, and snippets.

@bogomolov-dev
Last active August 29, 2015 14:19
Show Gist options
  • Save bogomolov-dev/cb2e102f2eb752849c21 to your computer and use it in GitHub Desktop.
Save bogomolov-dev/cb2e102f2eb752849c21 to your computer and use it in GitHub Desktop.
Kommagetrennte Liste in ein Array umwandeln und jeden Wert validieren
<?php
function getFormatedArray($string) {
$result = array();
if (strpos($string, ',') !== false) {
$substrings = explode(',', $string);
foreach ($substrings as $substring) {
if (trim($substring) != '') {
$result[] = strtoupper(trim($substring));
}
}
if (!count($result)) {
return 'ERROR';
}
}
else {
$result[] = $string;
}
return $result;
}
<?php
$result = array();
if (strpos($string, ',') !== false) {
$substrings = explode(',', $string);
// [...]
}
else {
$result[] = $string;
}
<?php
function getFormatedArray($string) {
$result = array();
$substrings = explode(',', $string);
foreach ($substrings as $substring) {
if (trim($substring) != '') {
$result[] = strtoupper(trim($substring));
}
}
if (!count($result)) {
return 'ERROR';
}
return $result;
}
<?php
function getFormatedArray($string) {
$result = explode(',', $string);
array_walk($result, function(&$substring, $key) {
$substring = strtoupper(trim($substring));
});
if (!count($result)) {
return 'ERROR';
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment