【问题标题】:Remove the int dynamically from create table using preg_replace使用 preg_replace 从创建表中动态删除 int
【发布时间】:2013-11-28 06:49:51
【问题描述】:

如何在 php 中使用 preg_replace 函数动态删除 int?

我有以下代码

CREATE TABLE vtiger_meter ( meterid int(11) DEFAULT NULL, meterno int(8) DEFAULT NULL, cdate date DEFAULT NULL, address varchar(255) DEFAULT NULL, customer varchar(100) DEFAULT NULL )

我想要下面的代码

CREATE TABLE vtiger_meter ( meterid int DEFAULT NULL, meterno int DEFAULT NULL, cdate date DEFAULT NULL, address varchar(255) DEFAULT NULL, customer varchar(100) DEFAULT NULL )

我想要输出。在 int 之后必须有一个动态值,我想删除那个。如何做到这一点任何建议?

我使用自定义函数 explode 但我想在 preg_replace 中使用它。

【问题讨论】:

  • 不为int字段指定长度是否有效?这就是您要删除的括号中的数字。这是对数据库的指令,以设置该 int 列以允许最多包含 11 个位置的数字。没有它,数据库要么选择默认值,要么不会创建表。
  • 是的,你是对的,我只是举例说明如何删除它?在 int 字段之后动态地可以有任何我想要 rmeove 的数字

标签: php preg-replace preg-match


【解决方案1】:
$input = 'CREATE TABLE vtiger_meter (
            meterid int(11) DEFAULT NULL,
            meterno int(8) DEFAULT NULL,
            cdate date DEFAULT NULL,
            address varchar(255) DEFAULT NULL,
            customer varchar(100) DEFAULT NULL )';
$output = preg_replace('/int\s*\(\s*\d+\s*\)/', 'int ', $input);
echo $output;

【讨论】:

  • 在 mysql 中可以显示 create table tablename 什么是 mssql server 2008 等效项?
  • 我不知道,我不使用sql-server。
【解决方案2】:

使用 preg_replace,将 int\(\d+\)\s* 替换为 int

$statement = preg_replace('/int\(\d+\)\s*/', 'int ', $statement);

注意'int'后面的空格。

【讨论】:

    【解决方案3】:

    你可以这样试试

    $str = "CREATE TABLE vtiger_meter ( meterid int(11) DEFAULT NULL, meterno int(25534) DEFAULT NULL, cdate date DEFAULT NULL, address varchar(255) DEFAULT NULL, customer varchar(100) DEFAULT NULL )";
    preg_replace("/ int\s*\(\s*([0-9]+)\s*\\)/", " int ", $str);
    print $str;
    

    输出是

    喜欢

    CREATE TABLE vtiger_meter ( meterid int DEFAULT NULL, meterno int DEFAULT NULL, cdate date DEFAULT NULL, address varchar(255) DEFAULT NULL, customer varchar(100) DEFAULT NULL ) 
    

    更多详情请查看此链接http://us2.php.net/preg_replace

    【讨论】:

    【解决方案4】:

    如果您只想替换 int 而不是 tinyintlongint 以及所有这些,您可以这样做(使用单词边界 \b 并替换不区分大小写)

    $str = preg_replace('/\bint\(\d+\)/i', 'int', $str);
    

    【讨论】:

      猜你喜欢
      • 2014-06-18
      • 1970-01-01
      • 2015-06-26
      • 2013-07-28
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 2013-09-12
      相关资源
      最近更新 更多