gpt4 book ai didi

php - 用括号外的逗号分解字符串

转载 作者:搜寻专家 更新时间:2023-10-31 21:47:26 25 4
gpt4 key购买 nike

我有一个字符串,我想用逗号展开,但前提是逗号没有嵌套在某些括号内。这是一个相当常见的用例,我一直在阅读该论坛中已回答的帖子,但并没有真正找到我要找的东西。

所以,详细来说:重点是,我有一个字符串(= SQL SELECT ... FROM 语句),我想从这个字符串中用逗号分隔的列表中提取元素(=要从中选择的列。但是,这些元素可以包含括号,并且有效地是函数调用。例如,在 SQL 中可以这样做

SELECT TO_CHAR(min(shippings.shippingdate), 'YYYY-MM-DD') as shippingdate, nameoftheguy FROM shippings WHERE ...

显然,我想要一个包含第一个元素的数组

TO_CHAR(min(shippings.shippingdate), 'YYYY-MM-DD') as shippingdate

作为第二个元素

nameoftheguy

到目前为止我采用的方法是 PHP and RegEx: Split a string by commas that are not inside brackets (and also nested brackets) , PHP: Split a string by comma(,) but ignoring anything inside square brackets? , Explode string except where surrounded by parentheses? , 和 PHP: split string on comma, but NOT when between braces or quotes?(专注于其中的正则表达式,因为我想用一个正则表达式行来完成),但在我的小测试区,那些没有给出正确的结果。事实上,他们都没有 split 或 split 太多:

$Input: SELECT first, second, to_char(my,big,house) as bigly, export(mastermind and others) as aloah FROM
$Output: Array ( [0] => first [1] => second [2] => to_char [3] => (my,big,house) [4] => as [5] => bigly [6] => export [7] => (mastermind and others) [8] => as [9] => aloah )

我的测试区代码是

<?php
function test($sql){
$foo = preg_match("/SELECT(.*?)FROM/", $sql, $match);
$bar = preg_match_all("/(?:[^(|]|\([^)]*\))+/", $match[1], $list);
//$bar = preg_match_all("/\((?:[^()]|(?R))+\)|'[^']*'|[^(),\s]+/", $match[1], $list);
//$bar = preg_match_all("/[,]+(?![^\[]*\])/", $match[1], $list);
//$bar = preg_match_all("/(?:[^(|]|\([^)]*\))+/", $match[1], $list);
//$bar = preg_match_all("/[^(,\s]+|\([^)]+\)/", $match[1], $list);
//$bar = preg_match_all("/([(].*?[)])|(\w)+/", $match[1], $list);
print "<br/>";
return $list[0];
}

print_r(test("SELECT first, second, to_char(my,big,house) as bigly, export(mastermind and others) as aloah FROM"));
?>

如您所想,我不是正则表达式专家,但如果可能的话,我想在一行中进行拆分。

最佳答案

按照这里的对话,我确实写了一个解析器来解决这个问题。它非常丑陋,但它可以完成工作(至少在某些限制范围内)。为了完整起见(如果其他人可能遇到同样的问题),我将其发布在这里:

function full($sqlu){
$sqlu = strtoupper($sqlu);
if(strpos($sqlu, "SELECT ")===false || strpos($sqlu, " FROM ")===false) return NULL;
$def = substr($sqlu, strpos($sqlu, "SELECT ")+7, strrpos($sqlu, " FROM ")-7);
$raw = explode(",", $def);
$elements = array();
$rem = array();
foreach($raw as $elm){
array_push($rem, $elm);
$txt = implode(",", $rem);
if(substr_count($txt, "(") - substr_count($txt, ")") == 0){
array_push($elements, $txt);
$rem = array();
}
}
return $elements;
}

当用下面的字符串喂它时

SELECT first, second, to_char(my,(big, and, fancy),house) as bigly, (SELECT myVar,foo from z) as super, export(mastermind and others) as aloah FROM table

返回

Array ( [0] => first [1] => second [2] => to_char(my,(big, and, fancy),house) as bigly [3] => (SELECT myVar,foo from z) as super [4] => export(mastermind and others) as aloah ) 

关于php - 用括号外的逗号分解字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55612680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com