这是当前位于一列中的数据:
“标题信息 - 公司此处 - 城市此处,ST”
ST代表状态即GA
有时我的数据如下所示:
“标题信息 - 公司此处 - 美国”
数据位于名为“title”的列上,并且不带引号。
如何使用 php 将其拆分为 4 个名为:
的新列“New_Title”、“公司”、“城市”、“州”
在第二个示例中,没有城市和州,只有国家/地区,可以转到“城市”列。
表架构:
CREATE TABLE table (
id int(8) NOT NULL DEFAULT '0',
Title char(100) DEFAULT NULL,
New_Title char(100) DEFAULT NULL,
Company char(100) DEFAULT NULL,
City char(100) DEFAULT NULL,
State char(100) DEFAULT NULL,
PRIMARY KEY (id)
)
ENGINE=MyISAM DEFAULT CHARSET=latin1;
感谢您的宝贵时间。
奥西
最佳答案
首先按“-”拆分,然后拆分按“,”返回的第三个元素
<?PHP
$title = $row['title']; // or however you get the title.
$parts = explode("-",$title);
if ( count($parts) != 3) die("Invalid title: $title");
$newtitle = $parts[0];
$company = $parts[1];
$location_parts = explode(',',$parts[2]);
$city = $location_parts[0];
$state = $location_parts[1];
//now you've got $newtitle, $company, $city, and $state to do something productive with.
关于php - 将数据拆分为多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1518325/