将数据拆分为多列 [英] Split data to multiple columns
问题描述
这是当前位于一列的数据:
This is the data currently located in one column:
标题信息 - 公司在这里 - 城市在这里,ST"
"Title Info - Company Here - City Here, ST"
ST 代表状态即 GA
ST represents state ie GA
有时我的数据如下所示:
Sometimes my data looks like this:
标题信息 - 公司在这里 - 美国"
"Title Info - Company Here - United States"
数据位于名为title"的列上,没有引号.
Data is on column named "title" and is without quotes.
我如何使用 php 拆分这个,命名为 4 个新列:
How do I split this using php, for 4 new columns named:
New_Title"、公司"、城市"、州"
"New_Title", "Company", "City", "State"
在没有城市和州只有国家的第二个示例中,可以转到城市"列.
On second example where there is no city and state just country, it would be ok to go to "city" column.
表架构:
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;
感谢您的时间.
奥西
推荐答案
先在-"上拆分,然后在,"上拆分第三个元素
First split on "-", then split the third element returned on ","
<?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.
这篇关于将数据拆分为多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!