拆分为数组,难点是 C(x, y("z", 2, 0)), 是一个整体。
将字符串 $s='A, B, C(x, y("z", 2, 0)), D, E';
拆分为数组,难点是 C(x, y("z", 2, 0)), 是一个整体。
想要的结果:
array(
'A',
'B',
'C(x, y("z", 2, 0))',
'D',
'E');
回复讨论(解决方案)
$s='A, B, C(x, y("z", 2, 0)), D, E';$keywords = preg_split("/\,\s(?=[A-Z])/", $s);var_dump($keywords);
$s='A, B, C(x, y("z", 2, 0)), D, E';$keywords = preg_split("/\,\s(?=[A-Z])/", $s);var_dump($keywords);
-----------------------------
非常感谢你的回复!但是靠?=[A-Z] 不准确,因为有可能括号内也是大写(嵌套的括号内不管什么内容是一个整体)。
$s='A, B, C(X, Y("Z", 2, 0)), D, E';
$s='A, B, C(X, Y("Z", 1, 0)), D(X, Y("Z", 2, 0)), E,F(X, Y("Z", 3, 0))';//提取获取里面的内容preg_match_all("/\(.*?\)\)/",$s,$match);//这部分正则可以自行修改$s = str_replace($match[0],'%',$s);//将整体的替换成某个符合,%百分号也可以自己选定$exs = explode(',',$s);$i = 0;foreach($exs as $key=>$value){ if (strpos($value,"%") !== false) { $exs[$key] = str_replace('%',$match[0][$i],$value); $i ++; }}var_dump($exs);
帮你在复杂化了
所以这种事情不是正则能够胜任的,老老实实写个函数比绞尽脑汁写正则实惠的多
$s='A, B, C(x, Y("z", 2, 0)), D, E';print_r(foo($s));function foo($s) { $r = array(); $m = 0; $t = ''; for($i=0; $i<strlen($s); $i++) { if($s{$i} == '(') $m++; if($s{$i} == ')') $m--; if($m == 0 && $s{$i} == ',') { if($t) $r[] = $t; $t = ''; }else $t .= $s{$i}; } if($t) $r[] = $t; return $r;}
Array( [0] => A [1] => B [2] => C(x, Y("z", 2, 0)) [3] => D [4] => E)
感谢2位的回复。
想到了一个匹配括号嵌套的正则,有类似计算的可以参考一下。(没有严谨测试)
$s='A, B,C(X, Y("Ez", 2, 0, Z(kk, 99))),D, E(u(8 , D(88)))';
preg_match_all('/[A-Z](?=[,])|[^,]*\(([^()]+|(?R))*\)/',$s,$z);
print_r($z[0]);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Shiba Inu, a once unstoppable force in the cryptocurrency world, has seen a phenomenal rise of 17,167,926.02% since September 1, 2020.

The cryptocurrency market is slightly in the green today (September 18), with numerous assets such as Bitcoin (BTC), Ethereum (ETH), and Binance Coin (BNB) charting

ETFSwap (ETFS) has taken a bullish turn in presale as its viral ICO token, predicted by a 22-year-old Solana millionaire as the next Dogecoin challenger set to rally 2000x in Q1 2025

Once dominating the meme coin scene, familiar favorites are now facing strong competition. Investors chasing substantial returns are shifting their gaze to new contenders with the potential for impressive gains. The crypto market is abuzz with specul

People think a bull run is coming to the crypto market. A prominent whale from Binance Coin (BNB) and Tron (TRX) is preparing for this event by loading up on Lunex Network (LNEX).

Investors and traders are now paying attention to Solana after an impressive comeback, allowing the digital currency to once again hit a two-month high.

As the cryptocurrency market continues to mature and evolve, new projects are entering the scene, each promising to deliver life-changing returns.

Binance announced spot trading support for Act I:The AI Prophecy (ACT), triggering more than a 2,224% explosion early on Monday.
