Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中从字符串中的括号中获取数字_Php_Mysql_Regex - Fatal编程技术网

如何在PHP中从字符串中的括号中获取数字

如何在PHP中从字符串中的括号中获取数字,php,mysql,regex,Php,Mysql,Regex,目前,我使用这样的阵列来控制Mysql数据库的版本: $pages_table = array ( "GUID" => array ( "type" => "CHAR(13)", "length" => 13, ) "Number" => array ( "type" => "TINYINT(4)", "length" => 4, ) "Pagename"

目前,我使用这样的阵列来控制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_类型字符串括号之间的数字:

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);
}
while($column=key($pages\u table)){
$new\u type=当前($pages\u table);
$new\u length=//从$new\u type获取此值;
if($existing_table[$column]['length']<$new_length){
$modify[$column]=$new\u类型;
}
下一步($pages\u table);
}

使用正则表达式:

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);
}
while($column=key($pages\u table)){
$new\u type=当前($pages\u table);
$hasLength=(preg\u match('/\(\d+\)/',$new\u type,$matches)==1);
$new_length=intval($matches[0]);
if($hasLength&$existing_table[$column]['length']<$new_length){
$modify[$column]=>$new\u类型;
}
下一步($pages\u table);
}

好的,我现在明白了。我仍然认为preg_match选项更具可读性。
$subject
是否需要
$new_type
?无法确定
$subject
来自何处,否则。@Tim抱歉,我忘记更新名称了。是的,它是
$new\u type
@Tim您的编辑被拒绝,因为它添加了更多的代码。编辑用于更正现有代码或改进语法/拼写。几秒钟前,我自己编辑了这篇文章,以便包含您编辑的一些想法;)@Itay:它是关于括号之间的整数,而不是字符串。所以他们的答案并不是100%符合我的需要。虽然regex部分是相同的,但我也不知道首先要使用regex,因为那个提问者有一个真正的regex问题。
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);
}