PHP:如何用逗号分隔字符串,而不用引号引起来? [英] PHP: How can I explode a string by commas, but not wheres the commas are within quotes?

查看:228
本文介绍了PHP:如何用逗号分隔字符串,而不用引号引起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将我的字符串输入分解为逗号处的数组.但是,该字符串在引号内包含逗号.

I need to explode my string input into an array at the commas. However the string contains commas inside quotes.

输入:

$line = 'TRUE','59','A large number is 10,000';

$linearray = explode(",",$line);
$linemysql = implode("','",$linearray);

将$ linemysql返回为:

Returns $linemysql as:

'TRUE','59','A large number is 10','000'

我如何才能做到这一点,而爆炸会忽略引号内的逗号?

How can I go about accomplishing this, with the explode ignoring the commas inside the quote marks?

推荐答案

由于使用的是逗号分隔的值,因此可以使用

Since you are using comma seperated values, you can use str_getcsv.

str_getcsv($line, ",", "'");

会返回:

Array
(
    [0] => TRUE
    [1] => 59
    [2] => A large number is 10,000
)

这篇关于PHP:如何用逗号分隔字符串,而不用引号引起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆