Nick Chubb
Nick Chubb

Reputation: 1483

Last Word From String - Explode()?

So I'm trying to get the last word from a string...

$pieces = explode(" ", $row["post_content"]);
print_r($pieces);
$customerEmail = array_pop($pieces);
echo "Email: " . $customerEmail."<br><br>";

$row["post_content"] = I wish I could be the icing on the cake..whipped snapbac50@gmail

Running the code gets me:

Array ( [0] => I [1] => wish [2] => I [3] => could [4] => be [5] => the [6] => icing [7] => on [8] => the [9] => cake..whipped [email protected] ) Email: cake..whipped [email protected]

I want the end of the array to echo just the email. Why does it include cake...whipped before the email if I array_pop()? I just want the email.

Upvotes: 1

Views: 627

Answers (1)

Brian
Brian

Reputation: 1908

Because the string contains whitespace characters other than spaces, you will have to use a different function: preg_split('/\s+/', $row['post_content']);

This will split the string using the pcre regular expression for one or more whitespace characters.

Upvotes: 1

Related Questions