php to extract a string from double quote

前端 未结 6 2009
灰色年华
灰色年华 2020-12-05 03:08

I have a string:

This is a text, \"Your Balance left $0.10\", End 0

How can I extract the string in between the double quotes an

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 03:32

    For everyone hunting for a full featured string parser, try this:

    (?:(?:"(?:\\"|[^"])+")|(?:'(?:\\'|[^'])+'));
    

    Use in preg_match:

    $haystack = "something else before 'Lars\' Teststring in quotes' something else after";
    preg_match("/(?:(?:\"(?:\\\\\"|[^\"])+\")|(?:'(?:\\\'|[^'])+'))/is",$haystack,$match);
    

    Returns:

    Array
    (
        [0] => 'Lars\' Teststring in quotes'
    )
    

    This works with single and double quoted string fragments.

提交回复
热议问题