如何在提交到 MySQL 之前使用 PHP 将表单中输入的文本转换为标题大小写?

问题描述 投票:0回答:3

我正在使用下面的 PHP 将数据从 html 表单提交到 MySQL 和电子邮件地址。 我希望除电子邮件字段之外的所有字段中输入的所有数据都转换为标题(正确)大小写。 我想知道是否有一种简单的方法可以做到这一点。 在搜索该网站时,我发现了这段代码:

function toTitleCase(str)
{
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

但我不确定这是否是我需要的,如果是我需要的,如何将其连接到现有代码。 有人可以帮忙吗?

谢谢,

尼克

<?php

$emailFrom = "****";
$emailTo = "****";
$subject = "****";

$body = "****" . "\n\n" . "Waged/Organisation Rate:" . "\n\n";
$row_count = count($_POST['name']);
$row_count2 = count($_POST['name2']);

$values = array();

 for($i = 0; $i < $row_count; $i++) {
     // variable sanitation...
     $name = trim(stripslashes($_POST['name'][$i]));
     $email = trim(stripslashes($_POST['email'][$i]));
     $organisation = trim(stripslashes($_POST['organisation'][$i]));
     $position = trim(stripslashes($_POST['position'][$i]));

     // this assumes name, email, and telephone are required & present in each element
     // otherwise you will have spurious line breaks. 
     $body .= "Name: " . $name . "    Email: " . $email . "  Organisation: " . $organisation . "   Position: " . $position . "\n\n";

     //prepare the values for MySQL
     $values[] = '(\'' . $name . '\',\'' . $email . '\',\'' . $organisation . '\',\'' . $position . '\')';

}

mysql_select_db($database, $connection);

$query1 = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES "  . implode(',', $values);

$result1 = mysql_query($query1);
if (!$result1) {
  die('Invalid query: ' . mysql_error());
}


 $body .= "Unwaged Rate:" . "\n\n";

 $values = array();

 for($i = 0; $i < $row_count; $i++) {
     // variable sanitation...
     $name = trim(stripslashes($_POST['name2'][$i]));
     $email = trim(stripslashes($_POST['email2'][$i]));

     // this assumes name, email, and telephone are required & present in each element
     // otherwise you will have spurious line breaks. 
     $body .= "Name: " . $name . "    Email: " . $email . "\n\n";

     //prepare the values for MySQL
     $values2[] = '(\'' . $name . '\',\'' . $email . '\')';
}
$query2 = "INSERT INTO conference (Name, Email) VALUES " . implode(',', $values2);

$result2 = mysql_query($query2);
if (!$result2) {
  die('Invalid query: ' . mysql_error());
}

// send email 
$success = mail($emailTo, $subject, $body, "From: <$emailFrom>");
?>
php javascript mysql forms title
3个回答
3
投票

使用

ucwords()
功能。


1
投票

使用 PHP 的

ucwords
函数。

http://php.net/manual/en/function.ucwords.php

示例:

$subject = "this is a subject";
$subject = ucwords($subject); //$subject is now "This Is A Subject"

0
投票

我不建议使用

ucwords()
,因为它会使每个单词的第一个字母大写,但在大多数情况下,您不希望像“to”和“and”这样的单词大写。

例如,这将被认为是正确的:“狐狸跳过原木。”

因此,您可以使用此功能:

/**
 * Converts $string to Title Case, and returns the result.
 */
function str_to_title_case($string) {
  // Words which shouldn't be capitalised
  $small_words = [
    'of','a','the','and','an','or','nor','but','is','if','then','else','when',
    'at','from','by','on','off','for','in','out','over','to','into','with'
  ];

  // Split the string into separate words
  $words = explode(' ', $string);

  foreach ($words as $key => $word) {
    // If this word is the first word, or it's not a small word
    if ($key === 0 || !in_array($word, $small_words)) {
      // Capitalise it
      $words[$key] = ucwords($word);
    }
  }

  // Join the words back into a string
  return implode(' ', $words);
}

改编并更新自 https://www.sitepoint.com/title-case-in-php/

© www.soinside.com 2019 - 2024. All rights reserved.