• Discussion
  • Data Parsing Issue: Extracting Information from Text Using PHP

I'm working on a data science project that involves parsing text data using PHP to extract specific information. The text data contains structured patterns, and I need to extract certain values from each entry. I'm using regular expressions to achieve this, but I'm encountering difficulties in getting the desired results. Here's a simplified example of what I'm trying to achieve:

Suppose I have a set of text entries like this:

Entry 1: Name: John Doe, Age: 30, Location: New York
Entry 2: Name: Jane Smith, Age: 25, Location: Los Angeles

I want to extract the values for Name, Age, and Location from each entry using PHP.

Here's the code I've tried so far:

<?php
$text = "Entry 1: Name: John Doe, Age: 30, Location: New York
Entry 2: Name: Jane Smith, Age: 25, Location: Los Angeles";

$pattern = '/Name: (.*?), Age: (.*?), Location: (.*)/';
preg_match_all($pattern, $text, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
    $name = $match[1];
    $age = $match[2];
    $location = $match[3];
    
    echo "Name: $name, Age: $age, Location: $location<br>";
}
?>

However, the code doesn't seem to match the pattern correctly, and I'm not getting any results. I suspect my regular expression might be incorrect, or there might be a better way to approach this parsing task.

I have checked on too many posts but could not find any solution, someone please review my code and provides guidance on how to correctly extract the information from the text entries using regular expressions in PHP? Alternatively, if there's a more efficient way to achieve this data parsing, I'd appreciate any suggestions. Thank you in advance for your help!

mark01
Congratulations on asking a PHP question in the PHP forums for a change. Third time you've been told about formatting your posts and the third time an irrelevant link had to be removed.

The code you posted works for me. What's it doing for you?
[Now let's see if this gets a reply]

    Hey, Weedpacket thanks for taking your time and looking into my concern and sorry to bother you I got the answer now. Thanks again

      mark01 Do you have any control over the formatting of the text input?
      Whoever is responsible should really be using XML.
      If there is any room to negotiate that I would recommend you pursue it.
      Far better than CSV or character delimitted line input.

      I have just ran your code and it works by the way.

      Write a Reply...