Display chosen parts of text

Hi,

I've got a text like so:
prefix: AUS
chart number: 7
suffix: B
international number: 1234
p panda na: P&A
ba folio number: A5002
arcs chart on update cd: V
arcs region: NE56
chart title: British Isles test title
chart scale: 1:50,000

Now I need to read each information separately without title, only data, example:
$prefix = 'AUS';
$chart = '7';
$suffix = 'B';
etc.

So we need to create a script doing job like so:
$prefix = [read data between 'prefix:' and 'chart number:'];
$chart = [read data between 'chart number:' and 'suffix:'];
$suffix = [read data between 'suffix:' and 'international number:'];
etc.

But I'm not sure how to do it. Any ideas?

Thanks for any help or hint.
LVL 8
ZadoAsked:
Who is Participating?
 
Ray PaseurCommented:
Zado: If this does not do what you want, please post a file that contains the test data.  Thanks, ~Ray

http://www.laprbass.com/RAY_temp_zado.php
Outputs:
Array
(
    [prefix] => AUS
    [chart number] => 7
    [suffix] => B
    [international number] => 1234
    [p panda na] => P&A
    [ba folio number] => A5002
    [arcs chart on update cd] => V
    [arcs region] => NE56
    [chart title] => British Isles test title
    [chart scale] => 1
)
<?php // RAY_temp_zado.php
error_reporting(E_ALL);


// SIMULATE READING A TEXT FILE WITH File_Get_Contents("path/to/data.txt")
$str = <<<ENDSTRING
prefix: AUS
chart number: 7
suffix: B
international number: 1234
p panda na: P&A
ba folio number: A5002
arcs chart on update cd: V
arcs region: NE56
chart title: British Isles test title
chart scale: 1:50,000
ENDSTRING;


// MAKE AN ARRAY FROM THE STRING
$arr = explode(PHP_EOL, $str);

// PROCESS THE ARRAY INTO AN ASSOCIATIVE ARRAY
$new = array();
foreach ($arr as $dat)
{
    $xyz = explode(':', $dat);
    $key = trim($xyz[0]);
    $val = trim($xyz[1]);
    $new[$key] = $val;
}

// SHOW THE NEW ARRAY
echo "<pre>";
print_r($new);

Open in new window

0
 
brad2575Commented:
Dont have the code at hand but read it into an array of times by line.

Then loop through the array of items and split by ":" character, then only display the second dimension of the array you just split up.
0
 
AriMcCommented:
Is the information you want always the rest of the line after the title never spanning to multiple lines?

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ZadoAuthor Commented:
brad2575: could you give an example, please?
AriMc: there may be multiple-line text, that's why I thought about reading text between text-A and text-B instead line by line.
0
 
AriMcCommented:
You could try this:

(place your data in data.txt in the same directory with this php-script)




<?php

function getdata($starttoken, $endtoken)
{
   $handle = fopen("data.txt", "r");
   $res = "";
   $inside = 0;

   while (($oneline=fgets($handle)) != FALSE)
   {
      if ($inside == 0)
      {
         if (($st=strpos($oneline, $starttoken, 0)) !== FALSE)
         {
            $res = substr($oneline, $st, -1)."\n";
            $inside = 1;
         }
      }
      else
      {
         if (($en=strpos($oneline, $endtoken, 0)) !== FALSE)
         {
            $res .= substr($oneline, 0, $en);
            $inside = 0;
         }
         else
         {
            $res .= $oneline."\n";;
         }
      }
   }

   fclose($handle);

   return($res);
}

echo getdata("chart number:", "suffix:");

?>

Open in new window

0
 
ZadoAuthor Commented:
I'll try it later, thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.