PHP は文字列を分解しますが、引用符で囲まれた単語を 1 つの単語として扱います

StackOverflow https://stackoverflow.com/questions/2202435

質問

次の文字列を分解するにはどうすればよいですか:

Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor

の中へ

array("Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor")

そのため、引用符内のテキストは単一の単語として扱われます。

今のところ私が持っているものは次のとおりです。

$mytext = "Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor"
$noquotes = str_replace("%22", "", $mytext");
$newarray = explode(" ", $noquotes);

しかし、私のコードは各単語を配列に分割します。引用符内の単語を 1 つの単語として扱うにはどうすればよいですか?

役に立ちましたか?

解決

あなたはpreg_match_all(...)を使用することができます:

$text = 'Lorem ipsum "dolor sit amet" consectetur "adipiscing \\"elit" dolor';
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $text, $matches);
print_r($matches);

生成されますされます:

Array
(
    [0] => Array
        (
            [0] => Lorem
            [1] => ipsum
            [2] => "dolor sit amet"
            [3] => consectetur
            [4] => "adipiscing \"elit"
            [5] => dolor
        )

)

そして、あなたが見ることができるように、それはまた、引用符で囲まれた文字列内のエスケープ引用符を占めています。

編集

短い説明:

"           # match the character '"'
(?:         # start non-capture group 1 
  \\        #   match the character '\'
  .         #   match any character except line breaks
  |         #   OR
  [^\\"]    #   match any character except '\' and '"'
)*          # end non-capture group 1 and repeat it zero or more times
"           # match the character '"'
|           # OR
\S+         # match a non-whitespace character: [^\s] and repeat it one or more times

そして%22に一致する代わりに、二重引用符の場合には、あなたがしたい:

preg_match_all('/%22(?:\\\\.|(?!%22).)*%22|\S+/', $text, $matches);

他のヒント

これは str_getcsv() にしてはるかに簡単だったでしょう。

$test = 'Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor';
var_dump(str_getcsv($test, ' '));

は、

与えます
array(6) {
  [0]=>
  string(5) "Lorem"
  [1]=>
  string(5) "ipsum"
  [2]=>
  string(14) "dolor sit amet"
  [3]=>
  string(11) "consectetur"
  [4]=>
  string(15) "adipiscing elit"
  [5]=>
  string(5) "dolor"
}

また、この複数の爆発機能を試すことができます。

function multiexplode ($delimiters,$string)
{

$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return  $launch;
}

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);

print_r($exploded);

いくつかの状況ではほとんど知られて token_get_all() に有用であることが分かるかもしれません:

$tokens = token_get_all("<?php $text ?>");
$separator = ' ';
$items = array();
$item = "";
$last = count($tokens) - 1;
foreach($tokens as $index => $token) {
    if($index != 0 && $index != $last) {
        if(count($token) == 3) {
            if($token[0] == T_CONSTANT_ENCAPSED_STRING) {
                $token = substr($token[1], 1, -1);
            } else {
                $token = $token[1];
            }
        }
        if($token == $separator) {
            $items[] = $item;
            $item = "";
        } else {
            $item .= $token;
        }
    }
}

結果:

Array
(
    [0] => Lorem
    [1] => ipsum
    [2] => dolor sit amet
    [3] => consectetur
    [4] => adipiscing elit
    [5] => dolor
)

私はこれと同様の複雑な文字列分割の問題を抱えてここに来ましたが、ここにある答えはどれも私が望んでいたものを正確に解決するものではなかったので、自分で答えを書きました。

他の人に役立つ場合に備えて、ここに投稿します。

これはおそらく非常に時間がかかり非効率的な方法ですが、私にとってはうまくいきました。

function explode_adv($openers, $closers, $togglers, $delimiters, $str)
{
    $chars = str_split($str);
    $parts = [];
    $nextpart = "";
    $toggle_states = array_fill_keys($togglers, false); // true = now inside, false = now outside
    $depth = 0;
    foreach($chars as $char)
    {
        if(in_array($char, $openers))
            $depth++;
        elseif(in_array($char, $closers))
            $depth--;
        elseif(in_array($char, $togglers))
        {
            if($toggle_states[$char])
                $depth--; // we are inside a toggle block, leave it and decrease the depth
            else
                // we are outside a toggle block, enter it and increase the depth
                $depth++;

            // invert the toggle block state
            $toggle_states[$char] = !$toggle_states[$char];
        }
        else
            $nextpart .= $char;

        if($depth < 0) $depth = 0;

        if(in_array($char, $delimiters) &&
           $depth == 0 &&
           !in_array($char, $closers))
        {
            $parts[] = substr($nextpart, 0, -1);
            $nextpart = "";
        }
    }
    if(strlen($nextpart) > 0)
        $parts[] = $nextpart;

    return $parts;
}

使い方は以下の通りです。 explode_adv 5 つの引数を取ります。

  1. ブロックを開く文字の配列 - 例: [, (, 、など。
  2. ブロックを閉じる文字の配列 - 例: ], ), 、など。
  3. ブロックを切り替える文字の配列 - 例: ", ', 、など。
  4. 次の部分への分割を引き起こす文字の配列。
  5. 作業対象の文字列。

この方法にはおそらく欠陥があります。編集は歓迎です。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top