fork(1) download
  1. <?php
  2.  
  3. $input = array(
  4. 'Test "test" test',
  5. "Test 'test' test",
  6. 'Test "test" "test"',
  7. "Test 'test' 'test'",
  8. "Test 'test' \"test\"",
  9. 'Test "te\\"st" test',
  10. "Test 'te\\'st' test",
  11. 'Test "te\'st" test',
  12. "Test 'te\"st' test",
  13. 'Test "te\'s\\"t" test',
  14. "Test 'te\"s\\'t' test",
  15. );
  16. $pattern = '/(["\'])(?:\\\\\1|(?!\1).)+\1/';
  17.  
  18. foreach($input as $str)
  19. {
  20. echo "Input: ", $str, "\n";
  21. preg_match_all($pattern, $str, $matches);
  22. print_r($matches);
  23. }
Success #stdin #stdout 0.03s 13064KB
stdin
Standard input is empty
stdout
Input: Test "test" test
Array
(
    [0] => Array
        (
            [0] => "test"
        )

    [1] => Array
        (
            [0] => "
        )

)
Input: Test 'test' test
Array
(
    [0] => Array
        (
            [0] => 'test'
        )

    [1] => Array
        (
            [0] => '
        )

)
Input: Test "test" "test"
Array
(
    [0] => Array
        (
            [0] => "test"
            [1] => "test"
        )

    [1] => Array
        (
            [0] => "
            [1] => "
        )

)
Input: Test 'test' 'test'
Array
(
    [0] => Array
        (
            [0] => 'test'
            [1] => 'test'
        )

    [1] => Array
        (
            [0] => '
            [1] => '
        )

)
Input: Test 'test' "test"
Array
(
    [0] => Array
        (
            [0] => 'test'
            [1] => "test"
        )

    [1] => Array
        (
            [0] => '
            [1] => "
        )

)
Input: Test "te\"st" test
Array
(
    [0] => Array
        (
            [0] => "te\"st"
        )

    [1] => Array
        (
            [0] => "
        )

)
Input: Test 'te\'st' test
Array
(
    [0] => Array
        (
            [0] => 'te\'st'
        )

    [1] => Array
        (
            [0] => '
        )

)
Input: Test "te'st" test
Array
(
    [0] => Array
        (
            [0] => "te'st"
        )

    [1] => Array
        (
            [0] => "
        )

)
Input: Test 'te"st' test
Array
(
    [0] => Array
        (
            [0] => 'te"st'
        )

    [1] => Array
        (
            [0] => '
        )

)
Input: Test "te's\"t" test
Array
(
    [0] => Array
        (
            [0] => "te's\"t"
        )

    [1] => Array
        (
            [0] => "
        )

)
Input: Test 'te"s\'t' test
Array
(
    [0] => Array
        (
            [0] => 'te"s\'t'
        )

    [1] => Array
        (
            [0] => '
        )

)