SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Preg_match help...

    ...with the following code:

    PHP Code:
    <?php

    $subject 
    "hello world all blah whatever";
    $pattern '/^blah/';
    preg_match($pattern$subject);
    print_r($matches);

    ?>
    I can't see through the fog here. What am I doing wrong?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Although the "$matches" parameter is optional, if you want to use it you need to include it in the preg_match() call.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    ^ specifies start of the string. You just need to remove that character from the pattern.

  4. #4
    SitePoint Member caribbean_hatch's Avatar
    Join Date
    Dec 2008
    Location
    Puerto Viejo, Costa Rica
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    you need both elements from the above posts - remove the caret and allow preg_match to stuff $matches with the result - otherwise $matches is an uninitialized var:
    $subject = "hello world all blah whatever";
    $pattern = '/blah/';
    preg_match($pattern, $subject, $matches);
    print_r($matches[0]);
    'Tis an ill wind which blows no minds.
    Puerto Viejo, Costa Rica

  5. #5
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As a good coding practice, you should also initialize $matches before you pass it by reference to preg_match.

    PHP Code:
    $subject "hello world all blah whatever";
    $pattern '/blah/';
    $matches = array();
    preg_match($pattern$subject$matches);
    print_r($matches[0]); 
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  6. #6
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys.

  7. #7
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, one more issue with this...

    First off, I keep getting this error message:
    Parse error: parse error, unexpected T_STRING
    At first, I had thought that I forgot to place a simple semi-colon or something, but in fact, I think it has to do with the way I tested my expression. Below is what I have so far:

    $subject ='1234 4332 /\foo\/\';
    $pattern = '/foo/';

    As you can see, I think it has to do with the '\' and '/', but as for WHY, I cannot say. Anyone have any ideas here? I think the slashes are escaping it, but I cannot discern how that is happening because I never understood expressions very well.

    Any help is appreciated.

  8. #8
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wolf_22 View Post
    Okay, one more issue with this...

    First off, I keep getting this error message:


    At first, I had thought that I forgot to place a simple semi-colon or something, but in fact, I think it has to do with the way I tested my expression. Below is what I have so far:

    $subject ='1234 4332 /\foo\/\';
    $pattern = '/foo/';

    As you can see, I think it has to do with the '\' and '/', but as for WHY, I cannot say. Anyone have any ideas here? I think the slashes are escaping it, but I cannot discern how that is happening because I never understood expressions very well.

    Any help is appreciated.
    You're escaping the last single quote in $subject by preceding it with a backslash.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  9. #9
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So does that constitute for a security issue? How would I overcome something like that?

  10. #10
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wolf_22 View Post
    So does that constitute for a security issue? How would I overcome something like that?
    It's a parse error, not a security flaw.

    When you use single quotes (hard quotes) the only thing you will need to escape are single quotes. Variables and other symbols are parsed litterally as strings.

    When you use double quotes (soft quotes) the main thing you'll need to escape are double quotes. Variables and other symbols are parsed and interpreted as PHP.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  11. #11
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhanson View Post
    It's a parse error, not a security flaw.

    When you use single quotes (hard quotes) the only thing you will need to escape are single quotes. Variables and other symbols are parsed litterally as strings.

    When you use double quotes (soft quotes) the main thing you'll need to escape are double quotes. Variables and other symbols are parsed and interpreted as PHP.
    So let's say that the "$subject" was a password to some form... There would be no way for that cracker to somehow use this "parse error" to his / her advantage?

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, because a parse error prevents your php script from running, ever. It makes your entire code/application useless.

    You can read here to learn proper php string syntax
    http://www.php.net/manual/en/language.types.string.php

  13. #13
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wolf_22 View Post
    So let's say that the "$subject" was a password to some form... There would be no way for that cracker to somehow use this "parse error" to his / her advantage?
    You don't understand what a parse error is.

    PHP is an interpreted language, so when you run a script the php code is translated into something your machine can read. A parse error is when the file contains something that is not valid syntactically.

    To help you understand, it would be like placing an naive British driver in an American car. Since he is naive he simply sits on the passenger side and therefore the car is never able to start.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  14. #14
    SitePoint Member caribbean_hatch's Avatar
    Join Date
    Dec 2008
    Location
    Puerto Viejo, Costa Rica
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL! A great analogy, bh... or maybe like routing the gasoline to the windshield wiper fluid ;^)
    'Tis an ill wind which blows no minds.
    Puerto Viejo, Costa Rica


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
  •