SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex to get Keyword

    Regex to get Keyword from google url

    PHP Code:
    $url "http://www.google.com/search?q=sitepoint&aq=f&aqi=g10";
    $phrase preg_match("/q=(.*)&{1}/",$url$m);
    echo 
    $m[0]; 
    That gives me something like
    ?q=sitepoint&aq=f&aqi=g10

    when all i want is
    sitepoint

    Any help would be much appreciated as always

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    See parse_url() and parse_str().

    PHP Code:
    <?php
    $elements 
    = array();

    parse_str(
        
    parse_url(
            
    'http://www.google.com/search?q=sitepoint&aq=f&aqi=g10',
            
    PHP_URL_QUERY
        
    ),
        
    $elements
    );

    print_r(
        
    $elements
    );

    /*
        Array
        (
                [q] => sitepoint
                [aq] => f
                [aqi] => g10
        )
    */
    ?>
    However, if you're insistent on still using RegExp you would be looking at something like...
    Code:
    ~(?<=[?&]q=)([^&]+)~i
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    The * is "greedy". Read more here: http://www.regular-expressions.info/repeat.html

    Try this expression: /q=(.*?)&/

    And you'll find what you need in $m[1]

  4. #4
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey that php worked just fine.

    You really are a php ninja.

    Me and my little brother loved ninjas. If you haven't yet check out the music vid "enter the ninja" from die antword. Its awesome

    Thanks so much man.

  5. #5
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    o and thanks for the other response guido.

    What answer is less resource intensive?

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Probably preg_match I'd say. Half as many function calls and a fairly basic/standard pattern too.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •