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.

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Beautiful. array_filter with a trim callback is a very useful piece of code.
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.

1 Comment

The alternative from @Progrock is even more sleek ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.