Link to home
Start Free TrialLog in
Avatar of rblampain
rblampain

asked on

Text to multi-dimension array

One of the experts provided the script below which works well. I've found this script is necessary for plain text documents and HTML formatted documents, so we need to modify it or make a second similar script for HTML documents to stop the rendering of the array values when using print_r or other. This is something completely out of my little knowledge of PHP.

A second modification suggested was to remove the naming of array levels which is not necessary.

And a third modification suggested is to save memory use by replacing the "foreach ..." by
reset($chapter);
while(list($key,$value) = each($chapter))
{
 //loop inner.
}

Is there someone interested in fixing this?

Your assistance to solve these problems is very much appreciated.
---------------------------------------------------------------------------------------------------------------------------------
Here is the script:
<?php

$text = "hello world!\nwelcome to some text. Some sentances and paragraphs, and some words.";

$chapter = array();
$paragraphs = explode("\n", $text);

foreach ($paragraphs as $para) {
  if ($para == "") continue;
  $addArray = array();
  $sentences = preg_split("/[\\.!?:;]+\\s+/", $para);

  foreach ($sentences as $sent) {
    if ($sent == "") continue;
    $addArray[] = preg_split("/\\s+/", $sent);
  }
  $chapter[] = $addArray;
}

print_r($chapter);
?>
ASKER CERTIFIED SOLUTION
Avatar of GrandSchtroumpf
GrandSchtroumpf

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial