为什么我的PHP函数没有返回值?

This function is meant to sanitise a given value but instead it outputs "n-a" as if no value was specified. It has to be the simplest issue but right this moment it has me beat.

function slug($text){ 

  // replace non letter or digits by -
  $text = preg_replace('~[^pLd]+~u', '-', $text);

  // trim
  $text = trim($text, '-');

  // transliterate
  $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

  // lowercase
  $text = strtolower($text);

  // remove unwanted characters
  $text = preg_replace('~[^-w]+~', '', $text);

  if (empty($text))
  {
    return 'n-a';
  }

  return $text;
}

I'd appreciate some input.

  1. Try using mb_string library instead of iconv. Its a better library.
  2. At each instance try var_dump or echo to make sure the return of the data.

You need to change your first regex which seems to be incorrect, should be,

// replace non letter or digits by -
$text = preg_replace('~[^\w\d]+~u', '-', $text);

Working Demo.