preg_match

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MB

    preg_match

    I want to be able to extract key/value pairs from a string but I am not
    succeeding. Experimenting and googling for a few hours didn't get me
    anywhere so I'm hoping for help here. My input string could look
    something like this:

    some_var=yellow another_var = "blue and red" third_var= 'pink'

    The values could be enclosed in single quotes, double quotes or no
    quotes at all as you can see. Is it possible to make a regular
    expression to extract these and if so, how?

    Thanks
    /MB
  • Captain Paralytic

    #2
    Re: preg_match

    On 14 Apr, 09:50, MB <n...@mail.comw rote:
    I want to be able to extract key/value pairs from a string but I am not
    succeeding. Experimenting and googling for a few hours didn't get me
    anywhere so I'm hoping for help here. My input string could look
    something like this:
    >
    some_var=yellow another_var = "blue and red" third_var= 'pink'
    >
    The values could be enclosed in single quotes, double quotes or no
    quotes at all as you can see. Is it possible to make a regular
    expression to extract these and if so, how?
    >
    Thanks
    /MB
    Why not start a bit further back. How dod you end up with such a
    string in the first place?

    Comment

    • MB

      #3
      Re: preg_match

      Captain Paralytic skrev:
      On 14 Apr, 09:50, MB <n...@mail.comw rote:
      >I want to be able to extract key/value pairs from a string but I am not
      >succeeding. Experimenting and googling for a few hours didn't get me
      >anywhere so I'm hoping for help here. My input string could look
      >something like this:
      >>
      >some_var=yello w another_var = "blue and red" third_var= 'pink'
      >>
      >The values could be enclosed in single quotes, double quotes or no
      >quotes at all as you can see. Is it possible to make a regular
      >expression to extract these and if so, how?
      >>
      >Thanks
      >/MB
      >
      Why not start a bit further back. How dod you end up with such a
      string in the first place?
      That's something I can't do anything about unfortunatly. I have to live
      with how this string looks and just have to solve it somehow.

      /MB

      Comment

      • pritaeas

        #4
        Re: preg_match


        "MB" <no@mail.comwro te in message
        news:yNFMj.6040 $R_4.4506@newsb .telia.net...
        >I want to be able to extract key/value pairs from a string but I am not
        >succeeding. Experimenting and googling for a few hours didn't get me
        >anywhere so I'm hoping for help here. My input string could look something
        >like this:
        >
        some_var=yellow another_var = "blue and red" third_var= 'pink'
        >
        The values could be enclosed in single quotes, double quotes or no quotes
        at all as you can see. Is it possible to make a regular expression to
        extract these and if so, how?
        >
        Thanks
        /MB
        Looks like HTML attributes, so here's a link:




        Comment

        • Alexey Kulentsov

          #5
          Re: preg_match

          MB wrote:
          I want to be able to extract key/value pairs from a string but I am not
          succeeding. Experimenting and googling for a few hours didn't get me
          anywhere so I'm hoping for help here. My input string could look
          something like this:
          >
          some_var=yellow another_var = "blue and red" third_var= 'pink'
          >
          The values could be enclosed in single quotes, double quotes or no
          quotes at all as you can see. Is it possible to make a regular
          expression to extract these and if so, how?
          >
          <?php


          $a='some_var=ye llow another_var = "blue and red" third_var= \'pink\'';

          // simple regexp without quoting
          preg_match_all( '/(\w+) *=
          *(\w+|(?:(["\'])([^"\']*)\\3))/',$a,$res,PREG_ SET_ORDER);

          foreach($res as $r)
          {
          echo $r[1].' = '.(isset($r[4])?$r[4]:$r[2])."\n";
          }

          ?>

          Comment

          • MB

            #6
            Re: preg_match

            Alexey Kulentsov skrev:
            MB wrote:
            >I want to be able to extract key/value pairs from a string but I am
            >not succeeding. Experimenting and googling for a few hours didn't get
            >me anywhere so I'm hoping for help here. My input string could look
            >something like this:
            >>
            >some_var=yello w another_var = "blue and red" third_var= 'pink'
            >>
            >The values could be enclosed in single quotes, double quotes or no
            >quotes at all as you can see. Is it possible to make a regular
            >expression to extract these and if so, how?
            >>
            >
            <?php
            >
            >
            $a='some_var=ye llow another_var = "blue and red" third_var= \'pink\'';
            >
            // simple regexp without quoting
            preg_match_all( '/(\w+) *=
            *(\w+|(?:(["\'])([^"\']*)\\3))/',$a,$res,PREG_ SET_ORDER);
            >
            foreach($res as $r)
            {
            echo $r[1].' = '.(isset($r[4])?$r[4]:$r[2])."\n";
            }
            >
            ?>
            That one works great. Just what i needed. Thanks!

            /MB

            Comment

            • MB

              #7
              Re: preg_match

              pritaeas skrev:
              "MB" <no@mail.comwro te in message
              news:yNFMj.6040 $R_4.4506@newsb .telia.net...
              >I want to be able to extract key/value pairs from a string but I am not
              >succeeding. Experimenting and googling for a few hours didn't get me
              >anywhere so I'm hoping for help here. My input string could look something
              >like this:
              >>
              >some_var=yello w another_var = "blue and red" third_var= 'pink'
              >>
              >The values could be enclosed in single quotes, double quotes or no quotes
              >at all as you can see. Is it possible to make a regular expression to
              >extract these and if so, how?
              >>
              >Thanks
              >/MB
              >
              Looks like HTML attributes, so here's a link:
              I should have thought of that. :-)
              I got that one to work well with some small adjustments. Thanks!

              /MB

              Comment

              Working...