0

I apologize if this question has a no brainer answer. I am still learning more ins and outs of php.

I have a snippet of code that is taking in a CSV file. This CSV file is uploaded by a user who downloads it from an external source. In the CSV file, the person's first name and last name is not split in separate columns. Therefore, in PHP the following is used:

$member_name = explode( " ", $application_data[5]);

The problem is that when this data is then used to render a PDF document to send a letter to the member, it cuts off their last name if their last name is two words.

The information is loaded into the PDF document with the first name and last name field by using:

$member_name[0],
$member_name[1]                         

Can I safely do:

$member_name[0],    
$member_name[1] + $member_name[2]

Even if 99% of the members do not have a space in the last name? Will I get an error that member_name[2] doesn't exist 99% of the time this is done?

I've done some searching on array_merge. Is that a better option? I've been trying to search for how php handles when you add something that doesn't exist and I'm drawing a blank.

I don't want to assume my solution will work and then when the person uploads their CSV file tomorrow, they get an error.

Or maybe I'm looking at this the wrong way and before it attempts to render a pdf document, I should do an if statement that figures out if $member_name[2] exists.

Thank you!

3
  • $member_name[1] . ($member_name[2] ?? "") will coalesce the null into an empty string without error. Commented Sep 29, 2017 at 20:27
  • 1
    Don't want to post it as an answer as it doesn't add anything to the thread but you can use array_column to get the first name and implode the "rest" of the array (last names). Commented Sep 29, 2017 at 20:44
  • @Andreas +1 for gratuitous use of array_column. Commented Sep 29, 2017 at 20:48

2 Answers 2

3

You can use the limit parameter of explode to only split at the first space.

$member_name = explode( " ", $application_data[5], 2);

Of course, if the first name also has more than one word, this still won't be quite right. Names are tricky.

Regarding array_merge, I don't think it would really be useful in this situation.

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

1 Comment

Awesome, thank you! I'll try this and see what the outcome is. I appreciate your quick response!
1

You could just use a limiter on your explode to only seperate on the first space. Here is an example.

$name = "George The King";
print_r(explode(' ', $name, 2)); //prints -> Array ( [0] => George [1] => The King )

1 Comment

It seems silly for me to not give you an upvote, considering we said basically the same thing. "This answer is useful" :)

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.