Text to multi-dimension array

Posted on 2005-05-17
Last Modified: 2012-05-05
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;

Question by:rblampain
    1 Comment
    LVL 30

    Accepted 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

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
    This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    The viewer will learn how to count occurrences of each item in an array.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now