目前我使用此类数组进行Mysql数据库的版本控制:
$pages_table = array (
"GUID" => array (
"type" => "CHAR(13)",
"length" => 13,
)
"Number" => array (
"type" => "TINYINT(4)",
"length" => 4,
)
"Pagename" => array (
"type" => "VARCHAR(30)",
"length" => 30,
)
它有效,但我想让它更干净,如:
$pages_table = array (
"GUID" => "CHAR(13)",
"Number" => "TINYINT(4)",
"Pagename" => "VARCHAR(30)",
);
然后,如果我遍历数组,我想将$ new_length(INT)设置为$ new_type字符串括号之间的数字:
while ($column = key($pages_table)) {
$new_type = current($pages_table);
$new_length = //Get this value from $new_type;
if ($existing_table[$column]['length'] < $new_length) {
$modify[$column] = $new_type;
}
next($pages_table);
}
答案 0 :(得分:2)
$new_length = (int) preg_replace('/\D/', '', $new_type);
答案 1 :(得分:2)
使用正则表达式:
preg_match('/\(\d+\)/', $subject, $matches);
$new_length = $matches[0];
如果确保字符串中没有其他数字,则可以缩短模式:
preg_match('/\d+/', $subject, $matches);
$new_length = $matches[0];
while ($column = key($pages_table)) {
$new_type = current($pages_table);
$hasLength = (preg_match('/\(\d+\)/', $new_type, $matches) == 1);
$new_length = intval($matches[0]);
if ($hasLength && $existing_table[$column]['length'] < $new_length) {
$modify[$column] => $new_type;
}
next($pages_table);
}