Getting text AFTER a matched find??

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Getting text AFTER a matched find??

Post by Matt Phelps »

I have a long string of data, this is some of it...

&endlon=-0.79721&lat=51.21175&lon=-0.79721&

....and the data I want to get from this string is the bit after the lat= part and before the next & . I'm not sure how to go about it. I can use a regular expression to find lat= but then I'm stuck.

Can anyone help?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

That isn't a URL?

Give your regex and we'll tell you what you're missing in it.
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

It was part of a URL yes. It's a string now.

I'm not too hot on regex. I've just been trying somehting like

ereg('lat=', $subject)

but that doesn't give me the data I want. I want the result to be 51.21175
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Oh. :-p

Have you tried looking into it? The PHP manual has a pretty good resource on regex as well. However, you would have to have at least seen so successful regex to be able to make sense out of the resource. Here:

Code: Select all

$str = preg_replace('/(.*)lat=([^&]*)(.*)/', '$2', $str);
Here's how it works:

(.*) Counts as any amount of characters. The "." means any character other than a newline, and "*" means 0 or more occurrences of it.

lat= A literal, and determines when the pattern before it is no longer in play.

([^&]*) Counts any amount of characters that aren't &. The "^" means "not" when it at the start of brackets. & is just a character to check against. The purpose of the brackets is to create a character class.

The reason I use parentheses is to make those values retrievable. The first set of parentheses can be accessed as $1, the second as $2, etc. The second parameter of preg_replace asks for what to replace it with, and I tell it to replace the entire string with only the contents of the second pair of parentheses.

I'd suggest reading into it more.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Sounds like something for parse_str()
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

feyd wrote:Sounds like something for parse_str()
Or what feyd says. Between you and me, I think he's memorized the manual. :-p
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

superdezign wrote:Or what feyd says. Between you and me, I think he's memorized the manual. :-p
For the most part, it's true.
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

superdezign wrote:

Code: Select all

$str = preg_replace('/(.*)lat=([^&]*)(.*)/', '$2', $str);
Thanks for the help.

Why the slashes at the front and end of the expression? Surely that means find slashes literally? I don't have any in the string.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

No, those are delimiters. Sorry, I assumed you knew since you seemed to be looking into regex.
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

Todays my first day with it.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

[\d] is a new characters class. So, it looks to match [^&]* first until it matches [\d]. And since you put [^&] and [\d] in the same pair or parentheses, they are both a part of $2.

Edit: Am I seeing things? I could have sworn your post just had code in it.
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

No sorry I deleted my post as I realised it didn' tmake sense!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Matt Phelps wrote:No sorry I deleted my post as I realised it didn' tmake sense!
:lol: Guess I'm too fast for you. :-p
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Just for clarification, \d matches digits. [0-9] is the equivalent.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

feyd wrote:Just for clarification, \d matches digits. [0-9] is the equivalent.
(Which I assume is why he tried to kind of toss it in there. :-p)
Post Reply