Regular expression Match :

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajd335
    New Member
    • Apr 2008
    • 123

    Regular expression Match :

    Hey all,
    I have some data like mg-st-005.xyz.pqr.dyd .
    I want to have the string mg-st-005 ( string before the first "."(dot)).
    My preg_match does not work properly....
    can you please help me?
    Thanks...
  • leeogrady
    New Member
    • Jun 2008
    • 11

    #2
    what is 'preg_match' ie what is the code and its purpose?
    are you just after code that will retrieve the string information before the first '.'?

    Comment

    • ajd335
      New Member
      • Apr 2008
      • 123

      #3
      Originally posted by leeogrady
      what is 'preg_match' ie what is the code and its purpose?
      are you just after code that will retrieve the string information before the first '.'?
      hey leeogrady,
      yes, I just wanted to have the string before the first ".".
      And for that I used preg match..
      My code was,
      Code:
      preg_match("/(.*)$./",$hostname,$matches);
      where host name is the mg-st-005.xyz.pqr.dyd
      Thanks...

      Comment

      • leeogrady
        New Member
        • Jun 2008
        • 11

        #4
        I am still not sure what preg_match is, but i think there is a better way...
        I would use strtok() a built in function to PHP
        the code is something like:
        Code:
        $delims = "/(.*)$/";
        $hostname = "mg-st-005.xyz.pqr.dyd";
        
        $word = strtok($hostname, $delims);
        if($word == "mg-st-005") {
          'IT IS A MATCH!
        }
        I hope this helps
        Lee

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by leeogrady
          I am still not sure what preg_match is, but i think there is a better way...
          I would use strtok() a built in function to PHP
          the code is something like:
          Code:
          $delims = "/(.*)$/";
          $hostname = "mg-st-005.xyz.pqr.dyd";
          
          $word = strtok($hostname, $delims);
          if($word == "mg-st-005") {
            'IT IS A MATCH!
          }
          I hope this helps
          Lee
          Would explode() not work?

          [php]
          $hostname = explode('.', 'mg-st-005.xyz.pqr.dyd ');

          echo $hostname[0];
          [/php]

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by leeogrady
            I am still not sure what preg_match is, but i think there is a better way...
            You've never used any of the preg_* functions? You use them in conjunction with regular expressions.

            preg_match(), preg_replace(), etc.

            Comment

            • leeogrady
              New Member
              • Jun 2008
              • 11

              #7
              No i havent!?! thanks for that markusn00b, i will look in to them!

              Dam. Yes of course, explode() is what i was trying to think of but couldnt! strtok() was the only thing i could think of!

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by leeogrady
                No i havent!?! thanks for that markusn00b, i will look in to them!

                Dam. Yes of course, explode() is what i was trying to think of but couldnt! strtok() was the only thing i could think of!
                When you get your head around regular expressions (I still haven't), you'll be able to be alot more effective in your programming.

                Darn that explode()!

                Comment

                • nashruddin
                  New Member
                  • Jun 2008
                  • 25

                  #9
                  Using explode() is the most efficient. But here's how to do that with preg_match:

                  Code:
                  <?php
                  $hostname = 'mg-st-005.xyz.pqr.dyd';
                  
                  preg_match("/^([^\.]+)\..+$/", $hostname, $matches);
                  
                  echo $matches[1]; /* will print: mg-st-005 */
                  ?>

                  Comment

                  • ajd335
                    New Member
                    • Apr 2008
                    • 123

                    #10
                    Thanks all,
                    The problem is solved..
                    Thanks for the help..

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Originally posted by ajd335
                      Thanks all,
                      The problem is solved..
                      Thanks for the help..
                      Glad we could all be of assistance!

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        I know I'm coming in here a little late, but...
                        [code=php]
                        $custom = substr($hostnam e, 0, strpos($hostnam e, '.'));
                        [/code]

                        Comment

                        • ajd335
                          New Member
                          • Apr 2008
                          • 123

                          #13
                          Originally posted by pbmods
                          I know I'm coming in here a little late, but...
                          [code=php]
                          $custom = substr($hostnam e, 0, strpos($hostnam e, '.'));
                          [/code]
                          Hey Pbmod,
                          Thanks for the solution..

                          Comment

                          Working...