1

I'm building a form that'll create a Word doc. There's a part where the user will create a list, and will separete the list lines by using the " | " (vertical bar) as a delimeter. I'm trying to explode() a string like this: "First line| Second line| Third and last line |". As you guys saw, I placed a vertival bar delimiter after the last line, that's 'cause the user will probably do this mistake, and it will generate a empty line on the list. I'm trying to avoid this error by using something like this:

$lines = explode("|",$lines);
for($a=0;$a<count($lines);$a++)
{
  if(!empty($lines[$a]) or !ctype_space($lines[$a])) 
  {
    //generate the line inside de Word Doc
  }
}

This code works when I create the string by my own while testing the code, but won't work when the string come from a Form. And keep generating a empty line list inside the Word Doc. When I var_dump() the $lines array it shows the last key as: [2]=> string(0) ""

I'm using Laravel and the form was created with the Form:: facade.(don't know if this matter, prob not)

If you guys could help me, I'd apreciate.

2 Answers2

1

Alternatively just use array_filter with the callback of trim to remove elements that are empty or contain spaces before you iterate.

<?php
$string = '|foo|bar|baz|bat|';
$split  = explode('|', $string);
$split  = array_filter($split, 'trim');
var_export($split);

Output:

array (
  1 => 'foo',
  2 => 'bar',
  3 => 'baz',
  4 => 'bat',
)

However you might not want to remove some empty values!

You could just trim off your pipes to begin with:

<?php
$string = '|foo|bar|baz|bat|';
$string = trim($string, '|');
$split  = explode('|', $string);
var_export($split);

Output as above.

Or use rtrim.

Progrock
  • 7,373
  • 1
  • 19
  • 25
  • Beautiful. [`array_filter`](http://php.net/manual/en/function.array-filter.php) with a [`trim`](http://php.net/manual/en/function.trim.php) callback is a very useful piece of code. – showdev Jun 30 '16 at 21:34
0

You may want to use PHP's && (and) rather than or.
For reference, see Logical Operators.

You only want to output the line if both empty() and ctype_space() return false. With your current code, blank strings will pass your if test because ctype_space() returns false even though empty() does not. Strings made up entirely of spaces will also pass because empty() returns false even though ctype_space() does not.

To correct the logic:

if(!empty($lines[$a]) && !ctype_space($lines[$a])) { ... }

Alternatively, I'd suggest trimming white space from the string before checking empty():

$lines = explode("|",$lines);

if (!empty($lines)) {
  foreach ($lines as $line) {
    if (!empty(trim($line)) {
      // output this line
    }
  }
}

Also, see 'AND' vs '&&' as operator.

Community
  • 1
  • 1
showdev
  • 28,454
  • 37
  • 55
  • 73
  • The [alternative](http://stackoverflow.com/questions/38133160/problems-with-empty-strings-empty-not-working#answer-38133591) from @Progrock is even more sleek ;) – showdev Jun 30 '16 at 21:38