Split a string ignoring quoted sections

前端 未结 13 2414
别跟我提以往
别跟我提以往 2020-12-06 00:15

Given a string like this:

a,\"string, with\",various,\"values, and some\",quoted

What is a good algorithm to split this based on

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 00:21

    I use this to parse strings, not sure if it helps here; but with some minor modifications perhaps?

    function getstringbetween($string, $start, $end){
        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0) return "";
        $ini += strlen($start);   
        $len = strpos($string,$end,$ini) - $ini;
        return substr($string,$ini,$len);
    }
    
    $fullstring = "this is my [tag]dog[/tag]";
    $parsed = getstringbetween($fullstring, "[tag]", "[/tag]");
    
    echo $parsed; // (result = dog) 
    

    /mp

提交回复
热议问题