Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple preg_match question

    Hi, Im trying to write a function and pass an argument that will be used by preg_match. can't get it to work and this seems to be where my problem is:

    $subject="54,1,2,3,51,55,4,5,6,5";

    $pattern ='/[,]*5[^\d]/';
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
    print_r($matches);

    //output: Array ( [0] => Array ( [0] => 5, [1] => 13 ) )

    $lookfor = "5";
    $pattern = "'/[,]*".$lookfor."[^\d]/'";
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
    print_r($matches);

    //output: Array ( )


    print_r($pattern);

    //output: '/[,]*5[^\d]/'
    Pattern is the same in both cases, so what am I missing here?

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    PHP Code:
     $pattern ='/[,]*5[^\d]/'//vs
    $pattern "'/[,]*".$lookfor."[^\d]/'"
    Notice the "' and the '" ? Simple mistake that is easy to overlook.
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #3
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cheers mate


  •  

    Posting Permissions

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