Page 1 of 1
Getting text AFTER a matched find??
Posted: Thu May 31, 2007 3:19 pm
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?
Posted: Thu May 31, 2007 3:23 pm
by superdezign
That isn't a URL?
Give your regex and we'll tell you what you're missing in it.
Posted: Thu May 31, 2007 3:28 pm
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
Posted: Thu May 31, 2007 3:49 pm
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.
Posted: Thu May 31, 2007 4:00 pm
by feyd
Sounds like something for
parse_str()
Posted: Thu May 31, 2007 4:03 pm
by superdezign
Or what feyd says. Between you and me, I think he's memorized the manual. :-p
Posted: Thu May 31, 2007 4:05 pm
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.
Posted: Thu May 31, 2007 4:07 pm
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.
Posted: Thu May 31, 2007 4:24 pm
by superdezign
No, those are delimiters. Sorry, I assumed you knew since you seemed to be looking into regex.
Posted: Thu May 31, 2007 4:31 pm
by Matt Phelps
Todays my first day with it.
Posted: Thu May 31, 2007 4:40 pm
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.
Posted: Thu May 31, 2007 4:43 pm
by Matt Phelps
No sorry I deleted my post as I realised it didn' tmake sense!
Posted: Thu May 31, 2007 4:47 pm
by superdezign
Matt Phelps wrote:No sorry I deleted my post as I realised it didn' tmake sense!

Guess I'm too fast for you. :-p
Posted: Thu May 31, 2007 7:35 pm
by feyd
Just for clarification, \d matches digits. [0-9] is the equivalent.
Posted: Thu May 31, 2007 7:48 pm
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)