Forum Moderators: coopster
For example:
$mybook="Last Night";
What I want to do is extract Last using a variable for the starting position and a variable for the ending location.
<?php
$mystring = "Hahn Jerry Don & Julia Fay";
$parts = explode("&",$mystring);
$username = trim($parts[0]);
$mate = trim($parts[1]);
$s = strpos($username," ");
$lastname = substr($username,0,$s);
$firstname = substr($username,($s+1));
print "<p>Last Name: ".$lastname."</p>";
print "<p>First Name(s): ".$firstname."</p>";
if ($mate)
{
print "<p>Mate: ".$mate."</p>";
}
else
{
print "<p>".$username." has no mate!</p>";
}
?>
WebmasterWorld have a way to post code that preserves white space formatting?
$mate = trim($parts[1]); $str = 'Hahn Jerry Don & Julia Fay';
preg_match('/^([^ ]+) (.*?)(?: & (.*))?$/', $str, $matches);
echo "Last name: {$matches[1]} - Husband: {$matches[2]}";
if(isset($matches[3])) {
echo " - Mate: {$matches[3]}";
}