Using regex and PHP, help!

Traste

Regular Member
Joined
Dec 18, 2011
Messages
303
Reaction score
26
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
 
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:
Back
Top