仅通过PHP中的外部delimter爆炸字符串

时间:2018-07-24 14:54:53

标签: php tokenize explode

我有一个来源,我从中获取经纬度对,它们之间用逗号分隔并用括号括起来,而对本身也用逗号分隔。我想获取每对经度并存储在表中

enter image description here

尝试过:

$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] */

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

4 个答案:

答案 0 :(得分:3)

也许

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

答案 1 :(得分:2)

您可以使用正则表达式吗? 我认为您可以尝试以下方法:

<?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;
}

答案 2 :(得分:1)

$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*)\)/模式捕获负或正的纬度和经度对

答案 3 :(得分:1)

您可以使用此

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

此功能结果将不受空白字符和空格的影响 例如 字符串

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

将被捕获,但是使用简单的爆炸可能会产生不合适的结果