-1

I have a string containing sentences. I wish to break these sentences into an array, then modify each array item, including removing any array items that are empty.

Here's what I have:

//Explode string by dot
$items_array = explode(".", $raw_data);

//Loop through the array
foreach ($items_array as $i => $item) {

  //Remove any whitespace at front of item
  $item= ltrim($item);

  //Check if array item is empty and unset
  if($item === NULL){
    unset($items_array[$i]); 
  }

  //Reinstate the dot
  $item .= '.';
}

However, this does not work. I see extra '.' and if I put a print(strlen($item)); within the loop (after the unset) I see a few 0 results.

I know the if condition is being met because if I put a print in there it will trigger the same amount of times a 0 appears, eg:

 if($item === NULL){
      print("no value");
      unset($raw_inclusions[$i]); 
    }

Have I done something wrong here?

Example $raw_data string. Assume I have no control over what is placed here.

$raw_data = "Brown fox. Lazy dog."

Expected/desired result:

$items_array = array("Brown fox.", "Lazy dog.");

Current result:

$items_array = array("Brown fox.", "Lazy dog.", ".");
MeltingDog
  • 14,310
  • 43
  • 165
  • 295
  • give us an example of you have and what you expect the program to do. – mbouzahir Mar 29 '17 at 00:26
  • @mbouzahir added above – MeltingDog Mar 29 '17 at 00:30
  • 1
    `$item` will always be a string after `$item= ltrim($item);` so afterwards `$item === NULL` can never be true. The line `$item .= '.';` at the end of the loop has no impact on `$items_array` unless you loop using a reference, i.e. `as &$item`. – faintsignal Mar 29 '17 at 00:45
  • @faintsignal gotta be honest here, I'm having trouble understanding. Sure so ltrim would turn it into a string, even if it's empty. Then shouldnt the if statement work if I use `strlen($item) == 0` wouldn't that work? – MeltingDog Mar 29 '17 at 00:51
  • @MeltingDog That's correct. – faintsignal Mar 29 '17 at 01:18

3 Answers3

1

It's actually pretty easy, you are just missing 1 line of code

Your Code

if($item === NULL){
    unset($items_array[$i]); 
}
//Reinstate the dot
$item .= '.';

Make it this

if($item === NULL){
    unset($items_array[$i]); 
}
else // <- The else is important
//Reinstate the dot
   $item .= '.';

And you need this line

$items_array[$i] = $item;

for anything to work (including your original code)

Forbs
  • 1,256
  • 1
  • 7
  • 9
0

It is unclear what you want to achieve, but following may help you further.

$raw_data = "Brown fox. Lazy dog.";
$items_array = preg_split('/(?<=[.?!])\s+(?=[a-z0-9])/i', $raw_data);

$sentences = new ArrayIterator($items_array);
for ($sentences->rewind(); $sentences->valid(); $sentences->next()) {
  // Do something with sentence
  print $sentences->current() . "\n";
}

And your approach to work it should look something like

//Explode string by dot
$items_array = explode(".", $raw_data);

//Loop through the array and pass sentence by reference
foreach ($items_array as $i => &$item) {

  //Remove any whitespace at front of item
  $item = ltrim($item);

  //Check if array item is empty and unset (and continue)
  if(empty($item)){
    unset($items_array[$i]);
    continue;
  }
  // Reinstate the dot
  $item .= '.';
}
mkungla
  • 3,390
  • 1
  • 26
  • 37
0

I would do this:

$items_array = array_map(function($v) { return ltrim($v).'.'; },
                         array_filter(explode('.', $raw_data)));
  • explode on .
  • filter empty items
  • map each item to trim and add .
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87