仅通过 PHP 中的外部定界符分解字符串

explode string only by outer delimter in PHP

我有一个来源,我从那里得到了一对纬度和经度,它们用逗号分隔并括在括号中,并且对本身也用 commas.I 分隔想要获取每一对并存储在table

尝试过:

$str = "(a,b),(c,d),(e,f),(g,h)"; //a,c,e,g = latitudes b,d,f,h = longitudes

$str = explode(',' , $str);

print_r($str);

期望

Array
(
    [0] => (a,b)
    [1] => (c,d)
    [2] => (e,f)
    [3] => (g,h)
)

实际:

Array
(
    [0] => (a
    [1] => b)
    [2] => (c
    [3] => d)
    [4] => (e
    [5] => f)
    [6] => (g
    [7] => h)
)

我的想法是,一旦获得所需的输出,我就可以遍历它们并..

$tmp = trim('(a,b)', '()'); //  "a,b"
$res = explode(',', $tmp);  //  ['a','b'] 
/* store $res[0] and $res[1] */

那么怎样才能得到想要的结果或者有什么更好的方法吗?

可能

$str = "(a,b),(c,d),(e,f),(g,h)"; //a,c,e,g = latitudes b,d,f,h = longitudes
$str = str_replace('),', ')|', $str);
$str = explode('|',  $str);
print_r($str);

Output:
Array
(
    [0] => (a,b)
    [1] => (c,d)
    [2] => (e,f)
    [3] => (g,h)
)
$str = "(a,b),(c,d),(e,f),(g,h)";
preg_match_all('/\(([^(),]),([^(),])\)/', $str, $matches);
print_r($matches[0]);
Array
(
    [0] => (a,b)
    [1] => (c,d)
    [2] => (e,f)
    [3] => (g,h)
)

更新

使用/\(((-?)\d*.\d*),((-?)\d*.\d*)\)/ 模式捕获负或正纬​​度和经度对

你会使用正则表达式吗? 我想你可以试试这个:

<?php 
// https://regex101.com/r/xiO0jy/2
// with latitude and longitude values
// this is just an example: 
$values = "(1.4,2.4),(3.33,7.89),(-7.123,9.01),(-4.58,-2.32)";

/* 
    The expression: \d*\.\d* is for decimal numbers 
    The expression: \( \) is for the parentheses
    The expression: -{0, 1} is for negative values
    The expression: (?: ) is to group the matches
*/
$regex = '/(?:\(-{0,1}\d*\.\d*,-{0,1}\d*\.\d*\))/';

preg_match_all($regex, $values, $matches, PREG_SET_ORDER, 0);

foreach($matches as $m) {
    echo $m[0] . PHP_EOL;
}

你可以用这个

function getCoord2($str,$explode=false){
    if(!is_string($str)) return [];
    $coord=[];
    $start=false;
    $latAndLong='';

    for($i=0,$strlen=strlen($str);$i<$strlen;$i++){
        if($str[$i]=='('){
            $start=true;
        }
        if($start){
            $latAndLong.=$str[$i];
        }
        if($start&&$str[$i]==')'){
            $start=false;
            if($explode){
                $coord[]=array_combine(['latitude','longitude'],array_map('trim',explode(',',trim($latAndLong,'()'))));
            }else{
               $coord[]=$latAndLong;
            }
            $latAndLong='';
        }


    }
    return $coord;
}

echo '<pre>';
print_r(getCoord2("(a,b),(c,d),(e,f),(g,h)",true));

此代码打印到屏幕

Array
(
    [0] => Array
        (
            [latitude] => a
            [longitude] => b
        )

    [1] => Array
        (
            [latitude] => c
            [longitude] => d
        )

    [2] => Array
        (
            [latitude] => e
            [longitude] => f
        )

    [3] => Array
        (
            [latitude] => g
            [longitude] => h
        )

)

print_r(getCoord2("(a,b),(c,d),(e,f),(g,h)"));

将打印

Array
(
    [0] => (a,b)
    [1] => (c,d)
    [2] => (e,f)
    [3] => (g,h)
)

此函数结果不受空白字符影响 space 例如 字符串

"(a,   b),    (c,
d),(e,f),(g,   h)" 

将按预期捕获,但使用简单的爆炸可能会产生不适当的结果