Web Analytics Made Easy -
StatCounter preg_match syntax - CodingForum

Announcement

Collapse
No announcement yet.

preg_match syntax

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • preg_match syntax

    Heyoooo-

    From php.net:
    PHP Code:
    <?php
    // get host name from URL
    preg_match("/^(http:\/\/)?([^\/]+)/i",
       
    "http://www.php.net/index.html"$matches);
    $host $matches[2];

    // get last two segments of host name
    preg_match("/[^\.\/]+\.[^\.\/]+$/"$host$matches);
    echo 
    "domain name is: {$matches[0]}\n";
    ?>
    What on EARTH are all those dashes slashes and carots? I know I will eventually need the preg_match but sheesh... I'm gettin dizzy....


  • #2
    Ah, matching is great fun!
    PHP Code:
    <?php
    // get host name from URL
    preg_match("/^([url]http://[/url])?([^/]+)/i",
       
    "http://www.php.net/index.html"$matches);
    $host $matches[2];

    // get last two segments of host name
    preg_match("/[^./]+.[^./]+$/"$host$matches);
    echo 
    "domain name is: {$matches[0]}\n";
    ?>
    ^ = start
    . = a character
    | = or
    % = does not have
    + = at least one
    ? = there may be one
    $ = end
    [] = a range - from...to

    Now, due to the way the VBulletin works, you have obviously had information stripped out as well. Your / blah blah /i indicates case insensitive.

    The first set of the search is looking for the [b]posibility of http://[b] and any number of /. Your new $host = www.php.net/index.html.
    the second is searching for at least one set of characters and / at the beginning, with one character. Since I'm assuming thats supposed to be \.\/ for the . in there, that becomes a period instead, its searching for the period, not a character.
    Voila, you end up with:
    domain name is: php.net
    Hope that helps you, it does take awhile to get used to it all.

    Wait a minute... something seems wrong with that.... should that be www not php.net for the echo out?

    Ok, yeah no thats right. I would have done a little different, but assuming the second parts are [^\.\/]+\.[^\.\/]+$ it will work fine.
    Last edited by Fou-Lu; Mar 8, 2004, 02:50 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment


    • #3
      Excellent... that list of all the symbols was what I was after, as well as the explanation. Fantastico. I was told that ".... is great fun" when I started in Flash with for() loops, so I figured I would get the same response.

      What is the forward slash mean?

      PHP Code:
      preg_match("/[^./]+.[^./]+$/",_$host,_$matches); 

      Comment


      • #4
        / would be searching for literal
        \ would escape special characters. Like if you wanted to look for something that contained a dollar sign, you need to state it as \$ otherwise it will search for the end of a string.


        Its matching it on each foundation of a set of characters divided by a / sign, the php.net/index.php for instance, in there it splits them in two, and returns $matches[0] as php.net.

        Also, numbers work a little bit different as well, so we won't get into that... yet...
        Last edited by Fou-Lu; Mar 9, 2004, 09:47 AM.
        PHP Code:
        header('HTTP/1.1 420 Enhance Your Calm'); 
        Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

        Comment

        Working...
        X