0

我正在使用这个 php 来解析数据并遍历行,但是逗号和双引号的配置让我非常困惑。

任何帮助将不胜感激。

我正在使用的这段代码破坏了其中包含逗号的字符串。

$getDataExploded = array();
$out = "";
foreach($getData as $getDataRows)
{
    $getDataExploded = explode(",",$getDataRows);
        $out .= '<div id="'.$getDataExploded[4].'" class="row"><span style="font-size:14px; color:#646464;">'.$getDataExploded[0].'</span><br><span style="font-size:12px; color:#3877D9;">'.$getDataExploded[1].' / '.$getDataExploded[2].'<br><em style="font-size:11px; color:#757575;">Posted: '.$getDataExploded[3].'</em></span></div>';


}

echo($out);

这是我要解析的数组的样子......

数组(29){ [0]=> 字符串(62)“'wefwefwef','ewfwefwef','wefwefwef',1/29/2013 9:09:04 PM,37”[1]=> 字符串(47) "'wefwef','wefwefwef','',2013 年 1 月 29 日 9:08:11 PM,36" [2]=> string(82) "'工作如你所不知','桑迪飓风','爱达荷州博伊西',1/29/2013 9:06:12 PM,35" [3]=> string(32) "'','','',1/29/2013 9:04:10 PM,34 " [4]=> string(49) "'wefwefwef','wfwefwef','',1/29/2013 9:02:05 PM,33" [5]=> string(82) "'警长招聘' ,'警长招聘','罗德岛',1/28/2013 7:13:53 PM,25" [6]=> string(81) "'社区销售代表','Trugreen','Warwick',1 /28/2013 7:13:53 PM,24" [7]=>string(86) "'警官招聘','克兰斯顿市','克兰斯顿',1/28/2013 7:13:53 PM,23" [8]=> string(144) "'RNs, LPNs , CNA, Nurse Practitioners and Physician Assistant Recruitment','Adil Business Systems, Inc.','Rhode Island',1/28/2013 7:13:53 PM,22" [9]=> string(79) "' Econotel EBS','Telecommunications Installer','Rumford',1/28/2013 7:13:53 PM,21" [10]=> string(73) "'Full-Charge Bookkeeper','Econotel EBS',' Rumford',1/28/2013 7:13:53 PM,20" [11]=> string(127) "'认证护理助理','晨星家庭护理','韦克菲尔德、韦斯特利、纽波特和纳拉甘西特', 2013 年 1 月 28 日下午 7:13:53,19" [12]=> string(78) "'汽车会计','殖民丰田','史密斯菲尔德',1/28/2013 7:13:53 PM,18" [13]=> string(89) "'认证护理助理','关于家庭护理的一切','Middletown',1 /28/2013 7:13:53 PM,17" [14]=> string(97) "'客户开发/内部销售','Hexagon Metrology','North Kingstown',1/28/2013 7:13: 53 PM,16" [15]=> string(85) "'RI Sheriff','RI Department of Public Safety','statewide',1/28/2013 7:13:53 PM,15" [16] => string(89) "'Certified Nursing Assistants','Morning Star Home Care','Warwick',1/28/2013 7:13:53 PM,14" [17]=> string(95) "'Entry -Level Software Developers','Hexagon Metrology','North Kingstown',1/28/2013 7:13:53 PM,13" [18]=> string(83) "'Electronics Technician','Purvis Systems Inc.','Middletown',1/28/2013 7:13:53 PM,12" [19]=> string(81) "'Nurse Practitioner','Memorial Hospital',' Plainville, MA',1/28/2013 7:13:53 PM,11" [20]=> string(86) "'家庭护理物理治疗师','纪念医院','Pawtucket',1/28/2013 7:13:53 PM,10" [21]=> string(87) "'Real Estate Specialist','Linear Title and Closing','Middletown',1/28/2013 7:13:53 PM,9" [22]=> string(77) "'设备技术员','Angelica Textiles','Pawtucket',1/28/2013 7:13:53 PM,8" [23]=> string(68) "'绘画机组负责人','LOPCO','罗德岛',1/28/2013 7:13:53 PM,7" [24]=> string(102) "'Diesel Mechanic','Interstate Diesel Equipment Service, Inc.','North Kingstown',1/28/2013 7:13:53 PM,6" [25]=> string(73) "'Hydromat Machine Operators','Greystone','Lincoln ',1/28/2013 7:13:53 PM,5" [26]=> string(85) "'全国销售经理','Hexagon Metrology','North Kingstown',1/28/2013 7:13 :53 PM,4" [27]=> string(131) "'北普罗维登斯试用警官招聘','北普罗维登斯镇','北普罗维登斯',2013 年 1 月 28 日下午 7:13:53, 3" [28]=> string(113) "'领土服务代表','Scotts Lawn Service','罗德岛和马萨诸塞州',2013 年 1 月 28 日下午 7:13:53,2" }string(73) "'Hydromat Machine Operators','Greystone','Lincoln',1/28/2013 7:13:53 PM,5" [26]=> string(85) "'全国销售经理',' Hexagon Metrology','North Kingstown',1/28/2013 7:13:53 PM,4" [27]=> string(131) "'北普罗维登斯见习警官招聘','北普罗维登斯镇', '北普罗维登斯',1/28/2013 7:13:53 PM,3" [28]=> string(113) "'领土服务代表','Scotts Lawn Service','罗德岛和马萨诸塞州',1/ 2013 年 2 月 28 日下午 7:13:53,2" }string(73) "'Hydromat Machine Operators','Greystone','Lincoln',1/28/2013 7:13:53 PM,5" [26]=> string(85) "'全国销售经理',' Hexagon Metrology','North Kingstown',1/28/2013 7:13:53 PM,4" [27]=> string(131) "'北普罗维登斯见习警官招聘','北普罗维登斯镇', '北普罗维登斯',1/28/2013 7:13:53 PM,3" [28]=> string(113) "'领土服务代表','Scotts Lawn Service','罗德岛和马萨诸塞州',1/ 2013 年 2 月 28 日下午 7:13:53,2" }1/28/2013 7:13:53 PM,4" [27]=> string(131) "'北普罗维登斯见习警官招聘','北普罗维登斯镇','北普罗维登斯',1/28/ 2013 年 7:13:53 PM,3" [28]=> string(113) "'领土服务代表','斯科特草坪服务','罗德岛和马萨诸塞州',2013 年 1 月 28 日下午 7:13:53 ,2" }1/28/2013 7:13:53 PM,4" [27]=> string(131) "'北普罗维登斯见习警官招聘','北普罗维登斯镇','北普罗维登斯',1/28/ 2013 年 7:13:53 PM,3" [28]=> string(113) "'领土服务代表','斯科特草坪服务','罗德岛和马萨诸塞州',2013 年 1 月 28 日下午 7:13:53 ,2" }

4

1 回答 1

1

PHP 提供了str_getcsv()一种您可能会觉得有用的方法。它使您无需担心逗号和文本分隔符。

$getDataExploded = array();
$out = "";
foreach($getData as $getDataRows)
{
    $getDataExploded = str_getcsv($getDataRows);
    //Do out stuff here `htmlentities` as required

}

echo($out);

编辑:更改了 jeroen 指出的 htmlentities 的使用

于 2013-01-31T22:14:03.150 回答