ItsPawl Posted January 13, 2011 Share Posted January 13, 2011 Hello, Im pretty new to PHP and completely new to Regex, so bare with me. Im trying to extract data in from a html and save in my own variables. For example from something like: <fruit="Carrots">Carrots<class="fruit">4,3<class="fruit">13,00 i would like to save the fruit name and corresponding price which is the second number (13,00 in this case). I would then like to find the same for all listed fruits. I was thinking that it might be possible to extract those strings by using regex to filter out the information i want, but i cant figure out how. The problem for me is to extract the price which i know to be the second number, but i cant find a unique string that preceeds it and the start index of the number may also vary. Appreciate any help. *Edit Sry, i just realized that there where a child forum for regex questions. Hope to get help anyway if its no problem. Quote Link to comment https://forums.phpfreaks.com/topic/224256-extracting-data-with-regex/ Share on other sites More sharing options...
thcx Posted January 13, 2011 Share Posted January 13, 2011 This looks a bit like XML. Have you tried parsing it with an XML parser? Quote Link to comment https://forums.phpfreaks.com/topic/224256-extracting-data-with-regex/#findComment-1158915 Share on other sites More sharing options...
ItsPawl Posted January 13, 2011 Author Share Posted January 13, 2011 Well, its not quite like that. I just wrote what came to mind. The pattern is the same as in the html though, so if there is any solution you can think of using regex then let me know. Trying to read up on regex, but seem like there is a lot to learn. Quote Link to comment https://forums.phpfreaks.com/topic/224256-extracting-data-with-regex/#findComment-1159037 Share on other sites More sharing options...
sasa Posted January 16, 2011 Share Posted January 16, 2011 try <?php $test = '<fruit="Carrots">Carrots<class="fruit">4,3<class="fruit">13,00'; preg_match_all('/<fruit="([^"]*)"\D+>[0-9,]+\D+>([0-9,]+)/', $test, $out); $out = array_combine($out[1], $out[2]); print_r($out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/224256-extracting-data-with-regex/#findComment-1160181 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.