【问题标题】:PHP: how to divide STRING by commas?PHP:如何用逗号分隔字符串?
【发布时间】:2021-05-15 14:57:09
【问题描述】:

如果我有字符串:

$_GET['string'] = 'array(1, 2, 3, 4, 5, "My friend Pol, Lucas and Pil", \'HTML without JS, CSS, PHP, JSP or others languages is ...\', 1, 2, 3)';

我如何在你的“LOGICS”值中划分这个字符串:

1
2
3
4
5
"My friend Pol, Lucas and Pil"
'HTML without JS, CSS, PHP, JSP or others languages is ...'
1
2
3

这个失败:

preg_split('/\'.*\'|".*"/', $STRING,  PREG_SPLIT_DELIM_CAPTURE);

逻辑上

explode(',', $_GET['string'])

还有

还有更多:如果 STRING 代表一个 ARRAY 多维呢?

【问题讨论】:

  • 为什么$_GET['string'] 会包含一个php 数组?您没有使用像 $_GET['string'] = '[1,2,3]' 这样的 JSON 数组是否有特定原因?
  • 感谢@MaartenDev 值来自 READ FILES of CONFIG: (config.php)
  • 那么为什么不使用包含/要求呢?

标签: php explode


【解决方案1】:

感谢团队, 这是我的解决方案:

$A = ARRAY();
$ARRAY = array_filter(preg_split('/(\'.*?\')|(".*?")/', $STRING, NULL, PREG_SPLIT_DELIM_CAPTURE));
foreach($ARRAY AS $i)if(preg_match('/^\'|^"/', $i))array_push($A, $i);
else {
    $i = explode(',', $i);
    foreach($i AS $ii)array_push($A, $ii);
    }
$A = array_filter(array_map('trim', $A));
print_r($A);

请问有什么改进吗?

问候

【讨论】:

  • 哦...这个“解决方案”失败了这个字符串:"enum('Cocos (Keeling) Islands','Colombia','Congo','Congo, the Democratic Republic of the','Cook Islands','Cote d''Ivoire','Croatia (Hrvatska)','Cuba','Cyprus','Czech Republic')";(取自 MySQL 的 ENUM 字段)。有什么解决办法?
  • 在文件phpMyAdmin-5.1.0-all-languages\libraries\classes\InsertEdit.php中找到函数getColumnEnumValues(),对该函数的注释说:“* @return array column values as an associative array”...检查一下!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 2015-03-09
  • 2016-09-04
相关资源
最近更新 更多