tags:

views:

98

answers:

9

I'm trying to display the apostrophe 's after the full name for example Samuel L. Jackson’s but if the last name or middle name is left out the 's is not displayed right for example Samuel ’s can some one help me correct this problem?

Thanks

Here is the PHP code.

if(!empty($first_name) || !empty($middle_name) || !empty($last_name)) {
    echo = $first_name . ' ' . $middle_name . ' ' . $last_name . ' \'s';
}
+1  A: 
echo trim($first_name . ' ' . $middle_name . ' ' . $last_name). ' \'s';

should do the trick?

One more issue: If you have a first and last name, there will be two spaces in between... is that going to be a problem at some point?

kander
if there is no middle name, then there will be two spaces between first name and the last name
marvin
@marvin HTML will correct the problem wouldn't it unless there is a better way?
meta
A: 
echo trim($first_name . ' ' . $middle_name . ' ' . $last_name) . '\s';

trim will get rid of any trailing spaces.

blockhead
A: 

Why are you using Single Quote ? You could simply use "'s" no need of escaping.

echo $first_name.(empty($middle_name) ? '' : $middle_name.' ').$last_name."'s"

alternative

$names = array($first_name);
if(!empty($middle_name))
  $names[] = $middle_name;
$names[] = $last_name;
echo implode(' ', $names)."'s";
A: 
$full_name = trim($first_name.' '.$middle_name);

if(!empty($full_name) && !empty($last_name)){
  $full_name .=' '.$last_name."'s";
  echo $full_name;
}
Sadat
$full_name cannot be blank as $first_name and $last_name can not be blank.
now its edited :)
Sadat
+4  A: 
$text = array();
if(!empty($first_name)) {
    $text[] = $first_name;
}
if(!empty($middle_name)) {
    $text[] = $middle_name;
}
if(!empty($last_name)) {
    $text[] = $last_name;
}

if(count($text) > 0) {
    echo implode(' ', $text).'\'s';
}
marvin
A: 
$a = ""
if (!empty($first_name)
  $a .= $first_name . " "
if (!empty($middle_name)
  $a .= $middle_name . " "
if (!empty($last_name) 
  $a .= $last_name . " 's"

This should do the trick.

James Black
A: 
$full_name = '';

if (!empty($first_name)) {
    $full_name .= $first_name;
}

if (!empty($middle_name)) {
    $full_name .= ' ' . $middle_name;
}

if (!empty($last_name)) {
    $full_name .= ' ' . $last_name;
}

$full_name = trim($full_name);

if (!empty($full_name)) {
    echo $full_name . "'s";
}
BoltClock
A: 
echo
  (empty($first_name) ? '' : $first_name) .
  (empty($middle_name) ? '' : ' ' . $middle_name) .
  (empty($last_name) ? '' : ' ' . $last_name) . "'s";

If the variables will always be set:

echo str_replace(
  '  ',
  ' ',
  $first_name . ' ' .
  $middle_name . ' ' .
  $last_name . "'s");

To display a proper single quote in HTML, replace the ' with ’, which displays as ’.

Mike
A: 

may be

echo htmlspecialchars($full_name, ENT_QUOTES);

will solve the problem

Col. Shrapnel