2

How do I remove the int dynamically using preg_replace function in php?

I have the following code

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 )

And I want the following code

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 )

I want like about output. After int there must a dynamic value I want to remove that one. How to do this any suggestion?

I do with custom function explode but I want using this in preg_replace.

Balaji Kandasamy
  • 4,446
  • 10
  • 40
  • 58
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
  • Is it valid to not specify a length for the int field? That is what the number in the parens that you want to remove is. It is an instruction to the database to set that int column up to allow numbers with up to 11 places. Without it, the database will either pick a default, or it will not create the table. – JC. Nov 28 '13 at 06:53
  • Yes you are right im just saying for an example how to remove it ?? dynamically after int the field can have any number i want to rmeove that one – I'm Geeker Nov 28 '13 at 06:55

4 Answers4

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;
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Using preg_replace, replace int\(\d+\)\s* with int.

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

Notice the space after 'int'.

SQB
  • 3,926
  • 2
  • 28
  • 49
0

You can try like this

$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;

Output is

like

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 ) 

For more detail please see this link http://us2.php.net/preg_replace

Siraj Khan
  • 2,328
  • 17
  • 18
  • In mysql it is possible to do show create table tablename what is the mssql server 2008 equivalent? – I'm Geeker Nov 28 '13 at 08:47
  • yes. Please see this link for more detail http://technet.microsoft.com/en-us/library/ms131327.aspx http://stackoverflow.com/questions/4586842/sql-comments-on-create-table-on-sql-server-2008 – Siraj Khan Nov 28 '13 at 10:20
0

If you want to replace only int but not tinyint or longint and all of these, you could do (using a word boundary \b and do the replace case-insensitive)

$str = preg_replace('/\bint\(\d+\)/i', 'int', $str);
Toto
  • 89,455
  • 62
  • 89
  • 125