php to extract a string from double quote

Multi tool use
Multi tool use












20















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 and have only the text (without the double quotes):




Your Balance left $0.10




I have tried preg_match_all() but with no luck.










share|improve this question

























  • You might find s($str)->between('"', '"') helpful, as found in this standalone library.

    – caw
    Jul 27 '16 at 2:34
















20















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 and have only the text (without the double quotes):




Your Balance left $0.10




I have tried preg_match_all() but with no luck.










share|improve this question

























  • You might find s($str)->between('"', '"') helpful, as found in this standalone library.

    – caw
    Jul 27 '16 at 2:34














20












20








20


10






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 and have only the text (without the double quotes):




Your Balance left $0.10




I have tried preg_match_all() but with no luck.










share|improve this question
















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 and have only the text (without the double quotes):




Your Balance left $0.10




I have tried preg_match_all() but with no luck.







php string preg-match preg-match-all double-quotes






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 18 '13 at 0:58









Just Plain High

767621




767621










asked Jun 19 '09 at 9:17









conandorconandor

1,37551833




1,37551833













  • You might find s($str)->between('"', '"') helpful, as found in this standalone library.

    – caw
    Jul 27 '16 at 2:34



















  • You might find s($str)->between('"', '"') helpful, as found in this standalone library.

    – caw
    Jul 27 '16 at 2:34

















You might find s($str)->between('"', '"') helpful, as found in this standalone library.

– caw
Jul 27 '16 at 2:34





You might find s($str)->between('"', '"') helpful, as found in this standalone library.

– caw
Jul 27 '16 at 2:34












6 Answers
6






active

oldest

votes


















55














As long as the format stays the same you can do this using a regular expression. "([^"]+)" will match the pattern




  • Double-quote

  • At least one non-double-quote

  • Double-quote


The brackets around the [^"]+ means that that portion will be returned as a separate group.



<?php

$str = 'This is a text, "Your Balance left $0.10", End 0';

//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/"([^"]+)"/', $str, $m)) {
print $m[1];
} else {
//preg_match returns the number of matches found,
//so if here didn't match pattern
}

//output: Your Balance left $0.10





share|improve this answer


























  • +1 simple and sweet

    – diEcho
    Apr 19 '13 at 5:53



















14














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.






share|improve this answer





















  • 3





    Works but is there a way to exclude the quotes itself from the returned string?

    – 3zzy
    Jul 25 '15 at 13:45



















9














Try this :



preg_match_all('`"([^"]*)"`', $string, $results);


You should get all your extracted strings in $results[1].






share|improve this answer
























  • +1 For providing a solution with preg_match_all

    – demongolem
    Aug 14 '12 at 15:39






  • 1





    This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.

    – Warre Buysse
    Apr 26 '14 at 2:31



















5














Unlike other answers, this supports escapes, e.g. "string with " quote in it".



$content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));





share|improve this answer































    0














    The regular expression '"([^\"]+)"' will match anything between two double quotes.



    $string = '"Your Balance left $0.10", End 0';
    preg_match('"([^\"]+)"', $string, $result);
    echo $result[0];





    share|improve this answer































      -7














      Just use str_replace and escape the quote:



      str_replace(""","",$yourString);


      Edit:



      Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1017051%2fphp-to-extract-a-string-from-double-quote%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        55














        As long as the format stays the same you can do this using a regular expression. "([^"]+)" will match the pattern




        • Double-quote

        • At least one non-double-quote

        • Double-quote


        The brackets around the [^"]+ means that that portion will be returned as a separate group.



        <?php

        $str = 'This is a text, "Your Balance left $0.10", End 0';

        //forward slashes are the start and end delimeters
        //third parameter is the array we want to fill with matches
        if (preg_match('/"([^"]+)"/', $str, $m)) {
        print $m[1];
        } else {
        //preg_match returns the number of matches found,
        //so if here didn't match pattern
        }

        //output: Your Balance left $0.10





        share|improve this answer


























        • +1 simple and sweet

          – diEcho
          Apr 19 '13 at 5:53
















        55














        As long as the format stays the same you can do this using a regular expression. "([^"]+)" will match the pattern




        • Double-quote

        • At least one non-double-quote

        • Double-quote


        The brackets around the [^"]+ means that that portion will be returned as a separate group.



        <?php

        $str = 'This is a text, "Your Balance left $0.10", End 0';

        //forward slashes are the start and end delimeters
        //third parameter is the array we want to fill with matches
        if (preg_match('/"([^"]+)"/', $str, $m)) {
        print $m[1];
        } else {
        //preg_match returns the number of matches found,
        //so if here didn't match pattern
        }

        //output: Your Balance left $0.10





        share|improve this answer


























        • +1 simple and sweet

          – diEcho
          Apr 19 '13 at 5:53














        55












        55








        55







        As long as the format stays the same you can do this using a regular expression. "([^"]+)" will match the pattern




        • Double-quote

        • At least one non-double-quote

        • Double-quote


        The brackets around the [^"]+ means that that portion will be returned as a separate group.



        <?php

        $str = 'This is a text, "Your Balance left $0.10", End 0';

        //forward slashes are the start and end delimeters
        //third parameter is the array we want to fill with matches
        if (preg_match('/"([^"]+)"/', $str, $m)) {
        print $m[1];
        } else {
        //preg_match returns the number of matches found,
        //so if here didn't match pattern
        }

        //output: Your Balance left $0.10





        share|improve this answer















        As long as the format stays the same you can do this using a regular expression. "([^"]+)" will match the pattern




        • Double-quote

        • At least one non-double-quote

        • Double-quote


        The brackets around the [^"]+ means that that portion will be returned as a separate group.



        <?php

        $str = 'This is a text, "Your Balance left $0.10", End 0';

        //forward slashes are the start and end delimeters
        //third parameter is the array we want to fill with matches
        if (preg_match('/"([^"]+)"/', $str, $m)) {
        print $m[1];
        } else {
        //preg_match returns the number of matches found,
        //so if here didn't match pattern
        }

        //output: Your Balance left $0.10






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 20 '17 at 14:52

























        answered Jun 19 '09 at 9:28









        Tom HaighTom Haigh

        48.6k18102135




        48.6k18102135













        • +1 simple and sweet

          – diEcho
          Apr 19 '13 at 5:53



















        • +1 simple and sweet

          – diEcho
          Apr 19 '13 at 5:53

















        +1 simple and sweet

        – diEcho
        Apr 19 '13 at 5:53





        +1 simple and sweet

        – diEcho
        Apr 19 '13 at 5:53













        14














        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.






        share|improve this answer





















        • 3





          Works but is there a way to exclude the quotes itself from the returned string?

          – 3zzy
          Jul 25 '15 at 13:45
















        14














        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.






        share|improve this answer





















        • 3





          Works but is there a way to exclude the quotes itself from the returned string?

          – 3zzy
          Jul 25 '15 at 13:45














        14












        14








        14







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jun 18 '13 at 0:45









        Just Plain High

        767621




        767621










        answered Aug 19 '11 at 14:50









        user426486user426486

        15114




        15114








        • 3





          Works but is there a way to exclude the quotes itself from the returned string?

          – 3zzy
          Jul 25 '15 at 13:45














        • 3





          Works but is there a way to exclude the quotes itself from the returned string?

          – 3zzy
          Jul 25 '15 at 13:45








        3




        3





        Works but is there a way to exclude the quotes itself from the returned string?

        – 3zzy
        Jul 25 '15 at 13:45





        Works but is there a way to exclude the quotes itself from the returned string?

        – 3zzy
        Jul 25 '15 at 13:45











        9














        Try this :



        preg_match_all('`"([^"]*)"`', $string, $results);


        You should get all your extracted strings in $results[1].






        share|improve this answer
























        • +1 For providing a solution with preg_match_all

          – demongolem
          Aug 14 '12 at 15:39






        • 1





          This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.

          – Warre Buysse
          Apr 26 '14 at 2:31
















        9














        Try this :



        preg_match_all('`"([^"]*)"`', $string, $results);


        You should get all your extracted strings in $results[1].






        share|improve this answer
























        • +1 For providing a solution with preg_match_all

          – demongolem
          Aug 14 '12 at 15:39






        • 1





          This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.

          – Warre Buysse
          Apr 26 '14 at 2:31














        9












        9








        9







        Try this :



        preg_match_all('`"([^"]*)"`', $string, $results);


        You should get all your extracted strings in $results[1].






        share|improve this answer













        Try this :



        preg_match_all('`"([^"]*)"`', $string, $results);


        You should get all your extracted strings in $results[1].







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 19 '09 at 9:28







        Arkh




















        • +1 For providing a solution with preg_match_all

          – demongolem
          Aug 14 '12 at 15:39






        • 1





          This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.

          – Warre Buysse
          Apr 26 '14 at 2:31



















        • +1 For providing a solution with preg_match_all

          – demongolem
          Aug 14 '12 at 15:39






        • 1





          This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.

          – Warre Buysse
          Apr 26 '14 at 2:31

















        +1 For providing a solution with preg_match_all

        – demongolem
        Aug 14 '12 at 15:39





        +1 For providing a solution with preg_match_all

        – demongolem
        Aug 14 '12 at 15:39




        1




        1





        This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.

        – Warre Buysse
        Apr 26 '14 at 2:31





        This is in fact the only one that works when you have multiple values between quotes in one string. Like the following: (a:2:{i:0;s:3:"149";i:1;s:3:"143";}) + the cleanest solution.

        – Warre Buysse
        Apr 26 '14 at 2:31











        5














        Unlike other answers, this supports escapes, e.g. "string with " quote in it".



        $content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));





        share|improve this answer




























          5














          Unlike other answers, this supports escapes, e.g. "string with " quote in it".



          $content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));





          share|improve this answer


























            5












            5








            5







            Unlike other answers, this supports escapes, e.g. "string with " quote in it".



            $content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));





            share|improve this answer













            Unlike other answers, this supports escapes, e.g. "string with " quote in it".



            $content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 19 '09 at 10:52









            KornelKornel

            75.9k28171233




            75.9k28171233























                0














                The regular expression '"([^\"]+)"' will match anything between two double quotes.



                $string = '"Your Balance left $0.10", End 0';
                preg_match('"([^\"]+)"', $string, $result);
                echo $result[0];





                share|improve this answer




























                  0














                  The regular expression '"([^\"]+)"' will match anything between two double quotes.



                  $string = '"Your Balance left $0.10", End 0';
                  preg_match('"([^\"]+)"', $string, $result);
                  echo $result[0];





                  share|improve this answer


























                    0












                    0








                    0







                    The regular expression '"([^\"]+)"' will match anything between two double quotes.



                    $string = '"Your Balance left $0.10", End 0';
                    preg_match('"([^\"]+)"', $string, $result);
                    echo $result[0];





                    share|improve this answer













                    The regular expression '"([^\"]+)"' will match anything between two double quotes.



                    $string = '"Your Balance left $0.10", End 0';
                    preg_match('"([^\"]+)"', $string, $result);
                    echo $result[0];






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 19 '09 at 9:26









                    Rich AdamsRich Adams

                    20.7k43058




                    20.7k43058























                        -7














                        Just use str_replace and escape the quote:



                        str_replace(""","",$yourString);


                        Edit:



                        Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.






                        share|improve this answer




























                          -7














                          Just use str_replace and escape the quote:



                          str_replace(""","",$yourString);


                          Edit:



                          Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.






                          share|improve this answer


























                            -7












                            -7








                            -7







                            Just use str_replace and escape the quote:



                            str_replace(""","",$yourString);


                            Edit:



                            Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.






                            share|improve this answer













                            Just use str_replace and escape the quote:



                            str_replace(""","",$yourString);


                            Edit:



                            Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jun 19 '09 at 9:19









                            PaulJWilliamsPaulJWilliams

                            16.3k14476




                            16.3k14476






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1017051%2fphp-to-extract-a-string-from-double-quote%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                E,pz15LR fk QOuOM425wmh,YgL1s,jBlyOBPnU5HdUAb02g3,qPh6u2FVllDDbWqhzGyB6uHizi74PxwDRFEM0xib,X4hw,He
                                8ZHTW0dBMebfCi,Ofr 7cGUujv7n,GDQ smwwLgscTHh,QAxTbGfdR03b,Ygcq

                                Popular posts from this blog

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas