To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
I've been going through a bunch of tutorials and looking at posts about regex, but I can't seem to figure out to use it. Any help would be greatly appreciated.
I'm trying to extract some text from a string, here's an example of the string:
$parse_string = "[{program:"30 Year Fixed $417,000",data:["4.750%","4.808%"]},{program:"15 Year Fixed $417,000",data:["4.125%","4.290%"]}]";
What I ultimate want is to break the string into variables so that I'd have an array of the program titles and then a multi dimensional array for the data.
Thanks in advance for any help, or if you've found a good tutorial, I'd love to read it.
First problem I see is that you are anchoring your regexp to the beginning ("^") and end ("$") of the string, so you need to remove those anchor characters. Also, you don't account for spaces in the contents between the quotes. A simpler way might be to just match on any non-quote:
Code:
'/program:"[^"]"/'
(Quoting the regexp in single quotes will then allow you to use double quotes within it without having to escape them.)
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett freelancer.internet.com Email me
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,128
Where's the text coming from? It looks almost – but not quite – like JSON. If it was JSON then json_decode could be used. But for it to be JSON, "program" and "data" would need to be quoted.
(If it's supposed to be JSON but you can't fix it, then
would turn it into a PHP structure; that assumes that (a) "program" and "data" only appear as member keys, and (b) only "program" and "data" appear as member keys).
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Last edited by Weedpacket; 11-07-2009 at 02:49 AM.
NogDog... I tried plugging that line in, and now I'm just getting the whole string. I'm not sure what's wrong.
Weedpacket... I really don't know anything about JSON, so I can't say for sure that it isn't. But what I'm doing is pulling mortgage rates from a table using:
I found that there's a Javascript variable (I'll post the whole string at the bottom of this post) on that site that sets up the table, so I'm trying to extract the variable to use in PHP. I figure that I could use a regex command to extract what I need, but I just can't get my head around regex. Everytime I think I've got it, it doesn't work... very frustrating!
Thanks again for the replies... any other suggestions would be greatly appreciated.
Here's the full section of code that I'm trying to work with:
Code:
<script type="text/javascript">
var data=[{program:"30 Year Fixed $417,000",data:["4.750%","4.794%","0.08","360","$2175","$3189","CitiMortgage Correspondent","4.625%","4.710%","0.55","360","$2143","$3189","CitiMortgage Correspondent","4.500%","4.633%","1.13","360","$2112","$3189","CitiMortgage Correspondent"]},
{program:"15 Year Fixed $417,000",data:["4.125%","4.272%","0.56","180","$3110","$3189","CitiMortgage Correspondent","4.000%","4.241%","1.20","180","$3084","$3189","CitiMortgage Correspondent","3.875%","4.215%","1.89","180","$3058","$3189","CitiMortgage Correspondent","3.750%","4.245%","2.98","180","$3032","$3189","CitiMortgage Correspondent"]},
{program:"30 Year Fixed $729,750",data:["5.000%","5.042%","0.15","360","$2952","$3189","CitiMortgage Correspondent","4.875%","4.961%","0.66","360","$2910","$3189","CitiMortgage Correspondent","4.750%","4.890%","1.29","360","$2869","$3189","CitiMortgage Correspondent","4.625%","4.846%","2.26","360","$2827","$3189","CitiMortgage Correspondent","4.500%","4.743%","2.54","360","$2786","$3189","CitiMortgage Correspondent","4.375%","4.653%","2.99","360","$2746","$3189","CitiMortgage Correspondent"]},
{program:"20 Year Fixed $417,000",data:["4.500%","4.592%","0.32","240","$2530","$3189","CitiMortgage Correspondent","4.375%","4.561%","1.14","240","$2503","$3189","CitiMortgage Correspondent","4.250%","4.525%","1.94","240","$2476","$3189","CitiMortgage Correspondent"]},{program:"10 Year Fixed $417,000",data:["4.125%","4.267%","0.21","120","$4073","$3189","CitiMortgage Correspondent","4.000%","4.334%","1.12","120","$4049","$3189","CitiMortgage Correspondent","3.875%","4.401%","2.04","120","$4026","$3189","CitiMortgage Correspondent","3.750%","4.504%","3.15","120","$4002","$3189","CitiMortgage Correspondent"]}]
</script>
Array
(
[0] => 30 Year Fixed $417,000
[1] => 15 Year Fixed $417,000
[2] => 30 Year Fixed $729,750
[3] => 20 Year Fixed $417,000
[4] => 10 Year Fixed $417,000
)
Is that along the lines you are looking for?
__________________
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
------------------------------------------------------------------------------------------------------
If preg was a woman, I'd marry her. But I could just see the pattern... she would replace me with someone with money ($1).
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,128
Quote:
Originally Posted by woodeye
Here's the full section of code that I'm trying to work with:
It does look like almost-JSON, then; so the second paragraph of my earlier reply applies.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
nrg_alpha - That works perfectly to get the program values, now I'm totally stumpped on how to extract the data variable into a multi-dimensional array. I was thinking that I should first extract the sections between the { }'s and then parse out the data variable into an array, but I can't even figure out how to extract the sections between the {}'s. I tried '#{ }#' but that didn't seem to work.
Am I reading your regex correctly?
Here's the regex: #program:"([^"]+)"#
I think the #'s are the delimiters, the program:" is the search value, the ()'s define the area to capture, the [^"]+ tell it to catch everything except quote marks. But what is the + for?
So if I'm right, then shouldn't this regex return something? #data:[([^"])]#
All I get is this error:
"Warning: preg_match_all() [function.preg-match-all]: Compilation failed: unmatched parentheses at offset 11 in /website/parse/check.php on line 17"
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,128
Typo on my part. that should be a $0.
Also, you're chopping off the '['...']' on the ends, which breaks it. So the substr() needs to be adjusted to retain them.
After all that array_chunk can be used to group elements in $data.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
I couldn't figure out how to make array_chunk work the way that I needed based on the results I was getting, so here's what I came up with. It probably isn't as elegant as it could be, but it gives me the results that I needed.