Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.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_Sql_Regex - Fatal编程技术网

使用php检查带有表值的正则表达式模式

使用php检查带有表值的正则表达式模式,php,sql,regex,Php,Sql,Regex,我有一个SQL表: date ************************ | id | date format | ************************ | 1 | 2013-02-10 | | 2 | 02-02-2013 | | 3 | 20130202 | ************************ 我想将diff(日期格式)存储在日期表中,根据表日期使用以下模式进行检查: /^\d{2}-\d{2}-\d{4}$/

我有一个SQL表:

date
************************
|  id   |  date format |
************************
|   1   | 2013-02-10   |
|   2   | 02-02-2013   |
|   3   | 20130202     |
************************
我想将diff(日期格式)存储在日期表中,根据表日期使用以下模式进行检查:

/^\d{2}-\d{2}-\d{4}$/

如何根据日期表中的
日期格式
字段检查此模式?

我不知道您为什么这样存储日期

我假设您的数据库是MySQL,如果是,您可以使用以下函数:

select `date format` from mytable where str_to_date(`date format`, '%d-%m-%Y') is not null;
请注意,此查询效率不高,因为它必须扫描每一行并应用
str\u to\u date
函数。

尝试此代码

通过此语法,您可以检查其正则表达式模式

  int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )

您可以通过稍微修改将regexp传递给查询本身

SELECT * FROM table WHERE `date format` REGEXP '^[0-9]{2}-[0-9]{2}-[0-9]{4}$'
我能修好它。。。(y)



你可以在MySQL中使用
REGEXP
首先,不要调用你的表类似于“date”之类的保留字。我不知道你为什么不使用
REGEXP
我想检查regex模式,它不是只固定日期检查,比如时间-日期-时间字符串等等。@user2496644,请查看链接文档。此函数返回
DATETIME
。但是,如果您特别想使用regex,那么DevZer0的答案应该可以帮您解决它的工作,但我的模式在工作上无效/^\D{2}-\D{2}-\D{4}$/您的模式包括
\D
限定符,该限定符在MySQL中无法识别,但我已经丢失了此类型的模式,所以我可以做什么?如果要将
\d
替换为
[0-9]
我将修复它…:D不要使用
@
符号。除了DevZer0的评论外,
mysql\uuquot>函数已被弃用,请开始查看
mysqli
PDO
<?php
$pattern = '/^\d{4}$/';
$link = mysql_connect('localhost', 'root', '');

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

if (!mysql_select_db('regex_field')) {
    die('Could not select database: ' . mysql_error());
}

$result = mysql_query('SELECT date_format FROM date');

$date = array();
$newdate = array();

while ($date_row = mysql_fetch_assoc($result)) {
    $date[] = $date_row;
}

foreach($date AS $key => $value) {
    $newdate[$key] = $value['date_format'];
}

foreach($newdate as $key => $value) {
    $testexample = @preg_match($pattern, $value, $matches);

    if($testexample == true) {
        echo "Example & Pattern Match";

        exit;
    }
}

echo "Example & Pattern MisMatch";

mysql_close($link);
?>