Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.