BenGoldberg Posted May 29, 2009 Share Posted May 29, 2009 I'm really horrible with regex but I need to use it for a site that I'm building, so that's why I'm here. Basically, I need to extract data between two quotation marks and store the value in a variable. An example of a string that I need to work on is... deg_F = "---.-" But I need to do this for quite a few lines. The cool thing is for every one, the data is inside the quotation marks. Another example... percent = "--" So the quotation marks don't always start at the same point since the strings are different, but the data I need is always inside the quotation marks. Any help would be greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/160104-solved-using-regex-to-get-data-between-two-quotation-marks/ Share on other sites More sharing options...
Adam Posted May 29, 2009 Share Posted May 29, 2009 This will return an array off all the values within quotes: preg_match_all('/"[^"]"/', $str, $matches); print_r($matches); Link to comment https://forums.phpfreaks.com/topic/160104-solved-using-regex-to-get-data-between-two-quotation-marks/#findComment-844817 Share on other sites More sharing options...
BenGoldberg Posted May 30, 2009 Author Share Posted May 30, 2009 I entered in a valid string with quotation marks, but I'm just getting an empty array for $matches. I guess it's not matching anything. ??? Link to comment https://forums.phpfreaks.com/topic/160104-solved-using-regex-to-get-data-between-two-quotation-marks/#findComment-845598 Share on other sites More sharing options...
sasa Posted May 30, 2009 Share Posted May 30, 2009 try preg_match_all('/"[^"]*"/', $str, $matches); print_r($matches); Link to comment https://forums.phpfreaks.com/topic/160104-solved-using-regex-to-get-data-between-two-quotation-marks/#findComment-845603 Share on other sites More sharing options...
Adam Posted May 30, 2009 Share Posted May 30, 2009 Ah yes my mistake, missed the * or +! Link to comment https://forums.phpfreaks.com/topic/160104-solved-using-regex-to-get-data-between-two-quotation-marks/#findComment-845690 Share on other sites More sharing options...
BenGoldberg Posted June 2, 2009 Author Share Posted June 2, 2009 Thanks you guys, it works just how I expect it to! The only thing is, is it possible to not copy the quotation marks? I just want the data inside them. Link to comment https://forums.phpfreaks.com/topic/160104-solved-using-regex-to-get-data-between-two-quotation-marks/#findComment-848161 Share on other sites More sharing options...
.josh Posted June 2, 2009 Share Posted June 2, 2009 preg_match_all('/"([^"]*)"/', $str, $matches); print_r($matches[1]); Link to comment https://forums.phpfreaks.com/topic/160104-solved-using-regex-to-get-data-between-two-quotation-marks/#findComment-848198 Share on other sites More sharing options...
BenGoldberg Posted June 4, 2009 Author Share Posted June 4, 2009 Ah yes, perfect. Thanks everyone! Link to comment https://forums.phpfreaks.com/topic/160104-solved-using-regex-to-get-data-between-two-quotation-marks/#findComment-849019 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.