Is there a way to get the max size of a varchar textfield? I have an entity with a textfield created using the core Fields feature that looks like this when I inspect the type of the field table in the database varchar(40).

I want to be able to determine max size of the string that can go into this field programmatically.  I.e. I want do know if there is a function I can call with suitable parameters, and it would return 40.

A Drupal API function would be the preferred answer, but a could live with a PHP function if this is not available in the Drupal API.

I was reminded in a comment (now deleted) that I previously had answered a very similar question.

However, that answer relies on the field in question being part of a schema.

This new question is about fields that has been created using the core Fields feature.  (I.e. using drupal_get_schema() will not fly.
There's no single function that will extract the value for you, but field_info_field() will get you an array that contains the data:

$info = field_info_field('field_foo');
if (isset($info['columns']['value']['length'])) {
  // Do something with $info['columns']['value']['length']
}


It it's a core text field, value will always be the name of the column you need to check. For other field types it may differ.