Skip to content

Instantly share code, notes, and snippets.

Created December 28, 2012 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4e7839d0eeb9183ef59c to your computer and use it in GitHub Desktop.
Save anonymous/4e7839d0eeb9183ef59c to your computer and use it in GitHub Desktop.
<?php
function getFooSqlFromBar($str) {
$ids = explode(',', $str);
if(!is_array($ids))
return null;
$ids = array_filter($ids, function($i) {return intval($i) > 0; });
if (count($ids) < 1)
return null;
return 'insert into tablula (id) values'. implode(',', array_map(function($i) { return '('.$i.')';}, array_unique($ids)));
}
// ---
echo getFooSqlFromBar(''), '<br />';
echo getFooSqlFromBar('1,2,3,4,5'), '<br />';
echo getFooSqlFromBar('a, ,2,2,2,2,2'), '<br />';
echo getFooSqlFromBar('1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment