Jump to content

Parsing Text


jadoo1989

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/183681-parsing-text/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/183681-parsing-text/#findComment-969508
Share on other sites

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);

?>

Link to comment
https://forums.phpfreaks.com/topic/183681-parsing-text/#findComment-969539
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.