使用php检测引号外的字符

时间:2021-07-22 21:46:28

I need to split a string into two using a delimiter character. All I have to do is use the explode() function... I know.

我需要使用分隔符将字符串分割为两个。我所要做的就是使用爆炸()函数…我知道。

But here is what I'm trying to do: I need to split a string using a delimiter but if the delimiter is enclosed in quotes it should be ignored.

但这里是我要做的:我需要使用分隔符分割一个字符串,但是如果分隔符包含在引号中,那么它应该被忽略。

Let's say my delimiter is a hyphen (-) and I need to split the following string:

假设我的分隔符是连字符(-),我需要拆分以下字符串:

The ‘big-yellow’ house-is near the lake

The first hyphen must be ignored because it is in quotes, therefore I would end up with two strings like these:

第一个连字符必须被忽略,因为它是在引号中,因此我将以如下两个字符串结束:

1. The ‘big-yellow’ house
2. is near the lake

And it also should be able to detect escaped quotes.

而且它还应该能够检测到转义引号。

E.g.: He doesn\’t like it because-he isn\’t from here.

In this case the hyphen is not within quotes therefore the string should be split.

在这种情况下,连字符不在引号内,因此字符串应该被分割。

Any thoughts?

任何想法吗?

2 个解决方案

#1


2  

You may use

你可以用

'[^'\\]*(?:\\.[^'\\]*)*'(*SKIP)(?!)|-

See regex demo

查看演示正则表达式

The '[^'\\]*(?:\\.[^'\\]*)*' part will match single quotes and any escaped entities, and (*SKIP)(?!) will force the regex engine to go on searching for matches after the last index + match length.

“[^ *(?:\ \ ' \ \]。[^ \ \]*)*的一部分将匹配单引号和任何逃脱的实体,和(*跳过)(? !)将迫使正则表达式引擎继续寻找匹配后索引+匹配长度。

And here is an IDEONE demo:

这是一个IDEONE demo:

$re = "/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'(*SKIP)(?!)|-/"; 
$strs = array("The 'big-yellow' house-is near the lake", "He doesn\'t like it because-he isn\'t from here."); 
foreach ($strs as $str) {
    $result = preg_split($re, $str);
    print_r($result);
}

Output:

输出:

Array( [0] => The 'big-yellow' house [1] => is near the lake) and Array( [0] => He doesn\'t like it because [1] => he isn\'t from here.).

数组([0]=>“大黄”房子[1]=>在湖边)和数组([0]=>他不喜欢它,因为[1]=>,他不是从这里来的)。

#2


1  

May be something like this?

可能是这样的?

function fsplit($str, $delimiter)
{
    $result = array();
    $inside_quote = false;
    $last_index = 0;
    for($i=0; $i<strlen($str);$i++)
    {
        if($str[$i] == $delimiter and !$inside_quote)
        {
            array_push($result, substr($str, $last_index, $i - $last_index));
            $last_index = $i+1;
        }
        elseif($str[$i] == "'")
        {
            $inside_quote = !$inside_quote;
        }

    }

    return $result;
}

#1


2  

You may use

你可以用

'[^'\\]*(?:\\.[^'\\]*)*'(*SKIP)(?!)|-

See regex demo

查看演示正则表达式

The '[^'\\]*(?:\\.[^'\\]*)*' part will match single quotes and any escaped entities, and (*SKIP)(?!) will force the regex engine to go on searching for matches after the last index + match length.

“[^ *(?:\ \ ' \ \]。[^ \ \]*)*的一部分将匹配单引号和任何逃脱的实体,和(*跳过)(? !)将迫使正则表达式引擎继续寻找匹配后索引+匹配长度。

And here is an IDEONE demo:

这是一个IDEONE demo:

$re = "/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'(*SKIP)(?!)|-/"; 
$strs = array("The 'big-yellow' house-is near the lake", "He doesn\'t like it because-he isn\'t from here."); 
foreach ($strs as $str) {
    $result = preg_split($re, $str);
    print_r($result);
}

Output:

输出:

Array( [0] => The 'big-yellow' house [1] => is near the lake) and Array( [0] => He doesn\'t like it because [1] => he isn\'t from here.).

数组([0]=>“大黄”房子[1]=>在湖边)和数组([0]=>他不喜欢它,因为[1]=>,他不是从这里来的)。

#2


1  

May be something like this?

可能是这样的?

function fsplit($str, $delimiter)
{
    $result = array();
    $inside_quote = false;
    $last_index = 0;
    for($i=0; $i<strlen($str);$i++)
    {
        if($str[$i] == $delimiter and !$inside_quote)
        {
            array_push($result, substr($str, $last_index, $i - $last_index));
            $last_index = $i+1;
        }
        elseif($str[$i] == "'")
        {
            $inside_quote = !$inside_quote;
        }

    }

    return $result;
}