Reputation: 4637
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.
Upvotes: 2
Views: 136
Reputation: 91430
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);
Upvotes: 0
Reputation: 2348
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 https://www.php.net/preg_replace
Upvotes: 0
Reputation: 4078
Using preg_replace, replace int\(\d+\)\s*
with int
.
$statement = preg_replace('/int\(\d+\)\s*/', 'int ', $statement);
Notice the space after 'int'.
Upvotes: 1
Reputation: 781096
$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;
Upvotes: 1