factoring2117 Posted April 24, 2009 Share Posted April 24, 2009 Hi, I have been working at this for days and can't seem to output the right info. Here is the string I am working with. $str = "<land><id>0</id><image_url>http://example.com</image_url><num_owned>0</num_owned><cost>4500</cost><name>Vacant Lot</name><income>$100</income></land> <land><id>1</id><image_url>http://www.example.com</image_url><num_owned>0</num_owned><cost>45000</cost><name>Empty Storefront</name><income>$300</income></land> <land><id>2</id>http://www.example.com</image_url><num_owned>0</num_owned><cost>900000</cost><name>Plaza</name><income>$2,000</income></land>"; I need to extract id, image_url, num_owned, cost, name, and income for each of these three. Here is my code so far: $preg = "#<land><id>(.*?)</id><image_url>(.*?)</image_url><num_owned>(.*?)</num_owned><cost>(.*?)</cost><name>(.*?)</name><income>(.*?)</income></land>#i"; if(preg_match_all($preg,$str,$match)) { foreach($match as $val) { echo $val[1]; } } I get the following when I run the script (inside the source code): <land><id>1</id><image_url>http://www.example.com</image_url><num_owned>0</num_owned><cost>45000</cost><name>Empty Storefront</name><income>$300</income></land>1http://www.example.com045000Empty Storefront$300 Here are the problems that I am having. 1) I need to be able to use the image, income, cost, name, for each one of the 3 land strings. I understand that if I make the $val[1] variable $val[2] I will output the second land string, but I need to use each one of the image, income, cost, name and income variables. How do I do this? 2) There are more than just 3 land strings so I need a regex string that will satisfy that demand. My ultimate goal would be to create a calculator to calculate which land is the best value to purchase. So in this particular case I need to take the cost/income= best_value I appreciate all your help before hand. Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 24, 2009 Share Posted April 24, 2009 Why not use simplexml ? Quote Link to comment Share on other sites More sharing options...
factoring2117 Posted April 25, 2009 Author Share Posted April 25, 2009 I would but there is other html involved in the string that I didn't input into the string listed above. I can keep trying to work with it, but that may be the best option. Anyone else have any better scenario? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted April 25, 2009 Share Posted April 25, 2009 Anyone else have any better scenario? Yeah, check the post above. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.