Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 拆分为数组,难点是 C(x, y("z", 2, 0)), 是一个整体。

拆分为数组,难点是 C(x, y("z", 2, 0)), 是一个整体。

Jun 23, 2016 pm 01:33 PM
2


将字符串 $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);
Copy after login
Copy after login

$s='A, B, C(x, y("z", 2, 0)), D, E';$keywords = preg_split("/\,\s(?=[A-Z])/", $s);var_dump($keywords);
Copy after login
Copy after login



-----------------------------
非常感谢你的回复!但是靠?=[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);
Copy after login

帮你在复杂化了

所以这种事情不是正则能够胜任的,老老实实写个函数比绞尽脑汁写正则实惠的多

$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;}
Copy after login
Array(    [0] => A    [1] =>  B    [2] =>  C(x, Y("z", 2, 0))    [3] =>  D    [4] =>  E)
Copy after login

感谢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]);

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dogecoin Trader Turns to RCO Finance (RCOF) After Shiba Inu (SHIB) Pullback Dogecoin Trader Turns to RCO Finance (RCOF) After Shiba Inu (SHIB) Pullback Aug 12, 2024 am 12:16 AM

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

Neiro (NEIRO) Skyrockets by 110% Daily, Becoming the 13th-Largest Meme Coin Neiro (NEIRO) Skyrockets by 110% Daily, Becoming the 13th-Largest Meme Coin Sep 18, 2024 pm 09:11 PM

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) Viral ICO Token Set To Outperform Dogecoin And Create Millionaires As 22-Year-Old Turned Solana Millionaire Predicts 2000x Rally In 2025 ETFSwap (ETFS) Viral ICO Token Set To Outperform Dogecoin And Create Millionaires As 22-Year-Old Turned Solana Millionaire Predicts 2000x Rally In 2025 Oct 18, 2024 am 04:38 AM

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

XYZVerse: The Next Meme Coin Champion! XYZVerse: The Next Meme Coin Champion! Oct 24, 2024 pm 07:12 PM

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

Lunex Network (LNEX) Set To See 18x Growth During Presale Lunex Network (LNEX) Set To See 18x Growth During Presale Oct 14, 2024 pm 04:30 PM

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).

Is Solana (SOL) Crypto Ripe for a Possible Huge Price Rally? Some Market Analysts Believe It Does Is Solana (SOL) Crypto Ripe for a Possible Huge Price Rally? Some Market Analysts Believe It Does Oct 23, 2024 am 09:08 AM

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.

FXGuys (FXG): The Next Dogecoin? Analysts Predict a 2,500% Rise by 2025 FXGuys (FXG): The Next Dogecoin? Analysts Predict a 2,500% Rise by 2025 Oct 14, 2024 pm 01:48 PM

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

Two Solana (SOL)-Based Memecoins Go Parabolic After Earning Sudden Support from Binance, the World's Largest Crypto Exchange Two Solana (SOL)-Based Memecoins Go Parabolic After Earning Sudden Support from Binance, the World's Largest Crypto Exchange Nov 12, 2024 am 06:16 AM

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

See all articles