Using regex and PHP, help!

 

Results 1 to 3 of 3
Hi, I have two kind of URLs: 1. /?s=search_term 2. /?s=search_term&product_type=product And then I have ...
  1. #1
    Traste is offline Regular Member
    Join Date
    Dec 2011
    Posts
    248
    Thanks
    14
    Thanked 8 Times in 8 Posts

    Default Using regex and PHP, help!

    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 Code:
    $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. #2
    rutix is offline Jr. VIP
    Join Date
    Sep 2012
    Posts
    103
    Thanks
    25
    Thanked 12 Times in 10 Posts

    Default Re: Using regex and PHP, help!

    preg_match("/\/\?s\=[a-z0-9_\-]+$/i", $url);

  3. The Following User Says Thank You to rutix For This Useful Post:

    Traste (12-08-2012)

  4. #3
    Traste is offline Regular Member
    Join Date
    Dec 2011
    Posts
    248
    Thanks
    14
    Thanked 8 Times in 8 Posts

    Default Re: Using regex and PHP, help!

    Quote Originally Posted by rutix View Post
    preg_match("/\/\?s\=[a-z0-9_\-]+$/i", $url);
    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 by Traste; 12-08-2012 at 06:38 PM.


Similar Threads

  1. How to go about this? REGEX & PHP
    By altschule in forum PHP & Perl
    Replies: 16
    Last Post: 05-20-2011, 10:59 PM
  2. RegEx Help
    By Crazy in forum General Scripting Chat
    Replies: 2
    Last Post: 02-25-2011, 09:53 PM
  3. Some simple PHP/RegEx Questions
    By altschule in forum PHP & Perl
    Replies: 6
    Last Post: 11-24-2010, 08:43 PM
  4. [HELP] Modify PHP/Regex WP Plugin - simple mod.
    By dheaven in forum General Programming Chat
    Replies: 0
    Last Post: 11-12-2009, 09:06 PM
  5. [Help] using regex
    By bzy39 in forum General Scripting Chat
    Replies: 4
    Last Post: 10-10-2009, 02:46 AM

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
  •  




BlackHatWorld on Twitter BlackHatWorld on FaceBook


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108