User login

Making sure multiple numeric ID input can be allowed in the most flexible way possible

Agaric knows that the best usability would never have people entering numeric IDs into a text field in the first place, but pending fancy remote search and completion techniques, we can at least take a set of numbers without rejecting them for using semicolons instead of commas.

<?php
/**
 * Replace all other separators in a string with spaces and split into arary.
 *
 * Agaric Utility function most practical for numeric IDs, rather than text.
 *
 * @param $string
 *  String value containing one or more simple values, separated by anything.
 * @return
 *  Array of values from the split string.
 */
function biblioreference_au_rationalize_separators($string) {
  // transform all separators into spaces
  // @REVIEW for pubmed ids we could replace all non-numeric with space
  $seps = array(',', ';', ':', '|', '.');
  $pmids = str_replace($seps, ' ', $form_state['values']['biblioreference_pubmed_id']);
  $string = trim(ereg_replace(' +', ' ', $string));
  return explode(' ', $string);
}
?>

Then checking that each of those is an actual integer is another step.

is_int is no good for checking form data, but there are a couple ways:

<?php
if (eregi("^[[:digit:]]+$", $presumed_integer_value) {
  return FALSE;
}
?>

or:

<?php
if (preg_match("/[^0-9]/", $presumed_integer_value)) {
  return FALSE;
}
?>

preg is usually faster, and apparently generally better.

Oh wait. There is a PHP function for checking that something is a straight-up integer, provided that it is given a string value (which forms always will). That function is

<?php
  ctype_digit($presumed_integer_value)
?>

http://us2.php.net/manual/en/function.ctype-digit.php

Resolution

Searched words: 
php collapse whitespace string Drupal securely handle form input over-engineer allow multiple pubmed ids so i make sure they can be allowed in the most flexible way possible Check if a number is an integer php validate comma separated integers faster ereg or preg drupal check integer

Comments

drerw

Oh wait. There is a PHP function for checking that something is a straight-up integer, provided that it is given a string value (which forms always will). That function is

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.