jadoo1989 Posted December 2, 2009 Share Posted December 2, 2009 I have a webpage which I have completely stripped of HTML and other code to have just plain text. This text is one long string with words delimited by spaces in the following format: Name: First Name Last Name Date Of Birth: 07/23/82 Member For 180 days. etc.. How can I parse this text so I can read over the the categories and extract the data into variables, even if the data is seperated by multiple words and even if the data itself is comprised of multiple words? Quote Link to comment https://forums.phpfreaks.com/topic/183681-parsing-text/ Share on other sites More sharing options...
monkeytooth Posted December 2, 2009 Share Posted December 2, 2009 explode() Quote Link to comment https://forums.phpfreaks.com/topic/183681-parsing-text/#findComment-969499 Share on other sites More sharing options...
rajivgonsalves Posted December 2, 2009 Share Posted December 2, 2009 A sample of your data would help ? Quote Link to comment https://forums.phpfreaks.com/topic/183681-parsing-text/#findComment-969502 Share on other sites More sharing options...
jadoo1989 Posted December 2, 2009 Author Share Posted December 2, 2009 Name: First Name Last Name Date Of Birth: 07/23/82 Member For 180 days. etc.. Is the data. So, Name: Bob Dole Date of Birth: 06/06/06 Member for: 180 days. Alias used in game: BobD Quote Link to comment https://forums.phpfreaks.com/topic/183681-parsing-text/#findComment-969503 Share on other sites More sharing options...
mikesta707 Posted December 2, 2009 Share Posted December 2, 2009 The exact data would be more helpful. IS that it, or is there more (It seems like there is more, you have that etc... there) Quote Link to comment https://forums.phpfreaks.com/topic/183681-parsing-text/#findComment-969504 Share on other sites More sharing options...
jadoo1989 Posted December 2, 2009 Author Share Posted December 2, 2009 An example would be: Name: Bob Dole Date of Birth: 06/06/06 Member for: 180 days. Alias used in game: BobD The full data would be miles and miles long considering I'm trying to parse sort of a profile page. The issue is multi-word categories and the fact that the data that needs to be stored could vary in amount of words from one profile to the next. Could I some way step through the string and parse values between categories? For instance, Say I'm starting with name and I make it store everything after Name to the beginning of Date of Birth and then make it store everything after Date of Birth to the beginning of Member for: so on and so forth throughout the entire string until all values are stored appropriately? The categories always appear by the same spelling and order, so they're static. Quote Link to comment https://forums.phpfreaks.com/topic/183681-parsing-text/#findComment-969508 Share on other sites More sharing options...
rajivgonsalves Posted December 2, 2009 Share Posted December 2, 2009 from the data you specified this should work <?php $str = 'Name: Bob Dole Date of Birth: 06/06/06 Member for: 180 days. Alias used in game: BobD'; preg_match("#Name: (.*?) Date of Birth: (.*?) Member for: (.*?) Alias used in game: ([^\b]+)#i", $str, $matches); print_r($matches); ?> Quote Link to comment https://forums.phpfreaks.com/topic/183681-parsing-text/#findComment-969539 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.