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 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts

    Custom preg_match_all()?

    How would I use preg_match_all in order to have this pattern:

    PHP Code:
    <?php
    $file1 
    file_get_contents("basketball.txt"); // $file1 = kobe&bryant?dwayne&wade
    preg_match_all("&?"$file1$matches);
    $select array_combine($matches[0], $matches[1]);
    ?>
    In order to output:
    PHP Code:
    $select[0] = "kobe"
    $select[1] = "bryant"
    $select[2] = "dwayne"
    $select[3] = "wade" 
    Thank you!
    Last edited by RonnyNishimoto; 08-16-2012 at 08:19 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You don't need to pcre at all, but IMO it does make it faster.
    Use a split:
    PHP Code:
    $s 'kobe&bryant?dwayne&wade';
    $split preg_split('/&|\?/'$s);
    print_r($split); 
    Results in:
    Code:
    Array
    (
        [0] => kobe
        [1] => bryant
        [2] => dwayne
        [3] => wade
    )

  • Users who have thanked Fou-Lu for this post:

    RonnyNishimoto (08-15-2012)

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    Thanks Fou!

    I was getting into weird territory like:

    PHP Code:
    /([a-z])&([a-z])/([a-z])?([a-z])/ 

  • #4
    New Coder
    Join Date
    Aug 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    $text
    ="kobe&bryant?dwayne&wade";

    $text=preg_match_all('/(\w+)/i',$text,$matches);

    print_r($matches[1]);
    ?>


  •  

    Posting Permissions

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