1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Hey Guest Last month we upgraded BlackHatWorld.com to a new platform - . If you notice anything that requires attention please start a new thread here.
    Dismiss Notice

Using regex and PHP, help!

Discussion in 'PHP & Perl' started by Traste, Dec 8, 2012.

  1. Traste

    Traste Regular Member

    Joined:
    Dec 18, 2011
    Messages:
    297
    Likes Received:
    20
    Hi,

    I have two kind of URLs:

    1. /?s=search_term
    2. /?s=search_term&product_type=product

    And then I have this code:

    PHP:
    $uri $_SERVER['REQUEST_URI'];
    if (
    preg_match("/^\?s=[a-zA-Z0-9_\-]$/"$uri)) {
                
    wp_register_style'wip_blog'get_template_directory_uri() . '/css/blog.css''''');
            }
    What I need to do is: "if the URL is "/?s=search_term", where search_term can be any alphanumeric character, then do the stuff. But if there is anything after the term (like &product_cat=) don't do anything.

    Any ideas using regex?

    Thanks
     
  2. rutix

    rutix Junior Member

    Joined:
    Sep 6, 2012
    Messages:
    102
    Likes Received:
    12
    preg_match("/\/\?s\=[a-z0-9_\-]+$/i", $url);
     
    • Thanks Thanks x 1
  3. Traste

    Traste Regular Member

    Joined:
    Dec 18, 2011
    Messages:
    297
    Likes Received:
    20
    Great! Works like a charm :) Thanks

    Update: Actually, when the search has more than one term it doesn't work. Any way to include the "+"?

    So, I can include " /?s=search_term+search_term2+search_term3".

    Update 2: Done! I just figured it out:
    preg_match("/\/\?s\=[a-z0-9_\-\+]+$/i", $url)
     
    Last edited: Dec 8, 2012