Getting text AFTER a matched find??
Moderator: General Moderators
-
Matt Phelps
- Forum Commoner
- Posts: 82
- Joined: Fri Jun 14, 2002 2:05 pm
Getting text AFTER a matched find??
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?
&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?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
Matt Phelps
- Forum Commoner
- Posts: 82
- Joined: Fri Jun 14, 2002 2:05 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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:
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.
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);(.*) 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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Sounds like something for parse_str()
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Or what feyd says. Between you and me, I think he's memorized the manual. :-pfeyd wrote:Sounds like something for parse_str()
-
Matt Phelps
- Forum Commoner
- Posts: 82
- Joined: Fri Jun 14, 2002 2:05 pm
Thanks for the help.superdezign wrote:Code: Select all
$str = preg_replace('/(.*)lat=([^&]*)(.*)/', '$2', $str);
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
Matt Phelps
- Forum Commoner
- Posts: 82
- Joined: Fri Jun 14, 2002 2:05 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
Matt Phelps
- Forum Commoner
- Posts: 82
- Joined: Fri Jun 14, 2002 2:05 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm