#1
  1. PHP is Simple & Great.
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    Pune ,India
    Posts
    266
    Rep Power
    12

    Regular expression , finding accurate pattern.


    Hello Geeks,

    I am facing some issues with a following Regular expressions.

    I would appreciate if someone can highlight what Accurate pattern shall I have to extract the "6Lef6gAAAAAAAPvrfh4xDy4hU4HwxNntiEtiMiNv" portion from the string.

    Please guide.

    Here is my script

    PHP Code:
    $pattern '/challenge?k=(.?*)/';
    $subject '<script type="text/javascript" src="http://api.example.com/challenge?k=6Lef6gAAAAAAAPvrfh4xDy4hU4HwxNntiEtiMiNv"></script>';

    preg_match($pattern$subject$key);
    echo 
    count($key); // returns 0 at this time. 
  2. #2
  3. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2006
    Location
    Hindustan
    Posts
    1,121
    Rep Power
    1309
    Hi there,

    Give this a try:


    PHP Code:
    <?php

    $pattern 
    '|src="http://api.example.com/challenge\?k\=(.*?)"|';
    $subject '<script type="text/javascript" src="http://api.example.com/challenge?k=6Lef6gAAAAAAAPvrfh4xDy4hU4HwxNntiEtiMiNv"></script>';

    preg_match_all($pattern$subject$matches);
    echo 
    "<pre>";
    print_r($matches); 
    echo 
    "</pre>";
    ?>
    Outputs

    Code:
    Array
    (
        [0] => Array
            (
                [0] => src="http://api.example.com/challenge?k=6Lef6gAAAAAAAPvrfh4xDy4hU4HwxNntiEtiMiNv"
            )
    
        [1] => Array
            (
                [0] => 6Lef6gAAAAAAAPvrfh4xDy4hU4HwxNntiEtiMiNv
            )
    
    )
    P.S : We do have a dedicated Regex forum for such queries.


    regards
    Trimbak D. Bankar.
  4. #3
  5. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    you need to escape your literal question mark, try this

    PHP Code:
    $pattern '/challenge\?k=(.*?)"/'
    $subject '<script type="text/javascript" src="http://api.example.com/challenge?k=6Lef6gAAAAAAAPvrfh4xDy4hU4HwxNntiEtiMiNv"></script>'

    preg_match($pattern,$subject,$key); 
    print_r($key); 
    gives
    Code:
    Array
    (
        [0] => challenge?k=6Lef6gAAAAAAAPvrfh4xDy4hU4HwxNntiEtiMiNv"
        [1] => 6Lef6gAAAAAAAPvrfh4xDy4hU4HwxNntiEtiMiNv
    )
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
    Originally Posted by tbankar
    P.S : We do have a dedicated Regex forum for such queries.
    Indeed we do.
  8. #5
  9. PHP is Simple & Great.
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    Pune ,India
    Posts
    266
    Rep Power
    12

    Thanks you geeks.


    Thank you geeks. I was escaping ? previously but I was trying

    (.?*) instead of ( .*?).

IMN logo majestic logo threadwatch logo seochat tools logo