1

Using this question, I'm experimenting with substr and strrpos and just cannot seem to get it right.

I have a name column coming from a MySQL database that contains both first and last name like this:

Doe, Jane A

The existing PHP echo looks like:

<?php echo($row_WADACustomerInfo['BillingName']); ?>

How do I strip the BillingName to only show Jane (firstname)?

2
  • What do you need to do exactly? You want to be able to remove the "Doe," and the "A"? Commented Jan 23, 2014 at 17:20
  • 1
    list($surname, $forename) = explode(',', $row_WADACustomerInfo['BillingName']); would be a good starting point Commented Jan 23, 2014 at 17:22

3 Answers 3

1

If you want to remove the last name, then the following works:

$explodeName = explode(',', $row_WADACustomerInfo['BillingName']);
$firstName = trim(array_pop($explodeName));
echo $firstName;
Sign up to request clarification or add additional context in comments.

Comments

0

Please use regular expression to locate the first name:

preg_match("/(\w+), (\w+)/", $row_WADACustomerInfo['BillingName'], $results);
echo $results[2];

Comments

0

As the full name contains three parts separated by a space ("Doe", "Jane,", and "A"), let's explode the variable to make an array. Then we take the second part as the first name. As arrays are indexed from zero, this would be the item number 1. So:

$aFullName = explode(" ", $row_WADACustomerInfo['BillingName']);
$firstName = $aFullName[1];
echo $firstName;

1 Comment

Please explain how your solution solves the problem to help future visitors to this question better understand.

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.