SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Regular expression meaning

    Hello on a tut i c the below code
    PHP Code:
    <?php
            
    /*** get the host name from a url ***/
            
    preg_match('#^(?:http://)?([^/]+)#i'"http://www.phpro.org/tutorials"$matches);

            
    /*** show the host name ***/
            
    echo $matches[1];
    ?>
    What's the meaning of
    Code:
    (?:

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    That defines a non capturing group, for example in your result for $matches http:// will never show up because it's been told to match but not be returned.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    That defines a non capturing group, for example in your result for $matches http:// will never show up because it's been told to match but not be returned.
    Thank you. I appreciate it

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Fuller details can be found in the PHP manual at http://php.net/regexp.reference.subpatterns
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    77
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just to break up your regular expression further:

    Delimiter
    Pattern Modifier
    Quantifier
    Sub-pattern
    Character Class
    Place Holder (Meta Character)
    Assertion
    String to match
    Anything but the following



    #^(?:http://)?([^/]+)#i


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •