• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

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
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:

$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;

1 Solution
You have two solutions here:  either you encode the tags, or you strip the tags:

$text = htmlentities("hello world!\nwelcome <b>to some text</b>. Some sentances and paragraphs, and some words.");
$text = strip_tags("hello world!\nwelcome <b>to some text</b>. Some sentances and paragraphs, and some words.");

Featured Post

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now