?
Solved

Display chosen parts of text

Posted on 2011-05-06
6
Medium Priority
?
194 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:Zado
6 Comments
 
LVL 16

Expert Comment

by:brad2575
ID: 35707484
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
 
LVL 9

Expert Comment

by:AriMc
ID: 35707507
Is the information you want always the rest of the line after the title never spanning to multiple lines?

0
 
LVL 8

Author Comment

by:Zado
ID: 35707567
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:AriMc
AriMc earned 1000 total points
ID: 35707669
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
 
LVL 8

Author Comment

by:Zado
ID: 35707733
I'll try it later, thanks.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 35707994
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Progress
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question