-1

目前我使用这样的数组来控制 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);
}
4

2 回答 2

2
$new_length = (int) preg_replace('/\D/', '', $new_type);
于 2013-09-07T15:39:02.850 回答
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);
}
于 2013-09-07T15:39:53.517 回答