Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,124 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,782 people online right now. Registration is fast and FREE... Join Now!




URL Regular Expression

 
Reply to this topicStart new topic

URL Regular Expression

skyhawk133
27 Feb, 2007 - 07:11 PM
Post #1

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 14,931



Thanked: 47 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
I'm having an issue with a regular expression. Here's what I've got:

$pattern = "/^.*" . $delimiter . "=([^&]+)&?.*\$/i";

This will take a referrer string that has search.php?a=1&b=whatever&q=what+you+searched+for&aq=t and break out just the $delimiter element. In this case the delimiter is "q" and I want whatever+you+searched+for. The problem is I'm getting "t" from the aq=t.

I would like it to only pull back ?q= or &q= but not sure how to add the ? and & in to the regex.





User is offlineProfile CardPM
+Quote Post

Spider
RE: URL Regular Expression
27 Feb, 2007 - 07:58 PM
Post #2

Arachnid
****

Joined: 10 Jul, 2002
Posts: 769


My Contributions
to match either ?q= or &q=, you could use
(\?|&)q=


User is offlineProfile CardPM
+Quote Post

snoj
RE: URL Regular Expression
27 Feb, 2007 - 08:49 PM
Post #3

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 6 times
Dream Kudos: 700
My Contributions
I'm not sure why that expression is the way it is...but would this work? (\?|&)q=([^&]+)
User is offlineProfile CardPM
+Quote Post

Spider
RE: URL Regular Expression
28 Feb, 2007 - 08:45 AM
Post #4

Arachnid
****

Joined: 10 Jul, 2002
Posts: 769


My Contributions
I pulled out a reference book for regex, and after a bit of fiddling I think this is what you want:
$pattern = '/(?:\?|&)'. $delimiter .'=([^&]+)/i';

then in theory (and it worked for all the values of $string that I tested)

preg_match($pattern, $string, $match);

should set $match[1] to what you want.

The ?: at the start is what prevents it from giving more stuff than you need (like you were getting the extra "t" from your original pattern.
User is offlineProfile CardPM
+Quote Post

Xmod
RE: URL Regular Expression
28 Feb, 2007 - 02:02 PM
Post #5

New D.I.C Head
*

Joined: 19 Jan, 2007
Posts: 35


My Contributions
This is from a code im workin on and it works. =) hopefully it will help

CODE

$url = "search.php?c=$c&s=$s&search_country=$_POST[search_country]&search_state=$_POST[search_state]&search_city=$_POST[search_city]&search_PropertyType=$_POST[search_PropertyType]&MinPrice=$min&MaxPrice=$max&rooms1=$rooms1&rooms2=$rooms2&bath1=$bath1&bath2=$bath2&AgentID=$agent&before=$before&school=$school&transit=$transit&park=$park&ocean_view=$ocean_view&lake_view=$lake_view&mountain_view=$mountain_view&ocean_waterfront=$ocean_waterfront&lake_waterfront=$lake_waterfront&river_waterfront=$river_waterfront";

    header("location:$url");
    exit();

User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: URL Regular Expression
28 Feb, 2007 - 02:30 PM
Post #6

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 14,931



Thanked: 47 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
That's not what I'm trying to do Xmod.


If you are logged out of dream.in.code and do a search on google for "C++ Sleep" and click the first result, you should see "C++ SLEEP" real big on the page you arrive at. It's taking what you searched for and displaying it on the page. The problem was with the reg ex I was using. I'm going to try Spiders and see if it works.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 10:14PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month