使用 php 将字符串的第一个字符大写。然后用XMLWriter输出。
这可能非常简单,但我不知道最好的方法。我正在使用 php 从 mysqli 数据库中提取数据来创建 XML 文档。我下面的代码有效,但“标题”字段中的数据全部大写。我只需要第一个字符大写,其余小写。我知道我需要 ucwords 功能,但还没有骰子。标题字段包含多个全部大写的单词。
我需要在数据进入 XML 区域之前使用 ucwords 对其进行格式化。我更喜欢在 php 中执行此操作,而不是更新数据库中的数据。感谢您的帮助!
<?php
// Connect to the database
global $link;
$link = mysqli_connect("localhost", "root", "pass", "database");
// Verify the connection worked
if (!$link) {
printf("Connection to database failed: %s\n", mysqli_connect_error());
exit();
}
// Prepare the database query
$stmt = mysqli_prepare($link, "SELECT * FROM table");
// Run the database query
mysqli_stmt_execute($stmt);
// Bind the result columns to PHP variables
mysqli_stmt_bind_result($stmt, $Name, $Title);
// Create a new XML document in memory
$xw = new xmlWriter();
$xw->openURI('php://output');
$xw->openMemory();
$xw->startDocument('1.0');
// Start the outer data container
$xw->StartElement('rss');
$xw->WriteAttribute('version', '2.0');
// Fetch values
while (mysqli_stmt_fetch($stmt)) {
{
$xw->startElement('item');
// Write out the elements
$xw->writeElement('Name', $Name);
$xw->writeElement('Title', $Title);
$xw->endElement();
}
// End container
$xw->endElement();
// End the document
$xw->endDocument();
//header('Content-Type: text/xml');
print $xw->outputMemory(true);
// Close the database statement
mysqli_stmt_close($stmt);
// Close the database connection
mysqli_close($link);
}
?>
This is probably real simple, but I don't know best way to do this. I am pulling data from a mysqli database with php to create an XML document. My code below works but data in the Title field is all caps. I need just the first character capitalized, the rest lowercase. I know I need the ucwords function, but no dice yet. The Title field has more than one word in all caps.
I need the data formatted by ucwords before it goes into the XML area. I prefer to do this in the php instead of updating the data in the db. Thanks for the help!
<?php
// Connect to the database
global $link;
$link = mysqli_connect("localhost", "root", "pass", "database");
// Verify the connection worked
if (!$link) {
printf("Connection to database failed: %s\n", mysqli_connect_error());
exit();
}
// Prepare the database query
$stmt = mysqli_prepare($link, "SELECT * FROM table");
// Run the database query
mysqli_stmt_execute($stmt);
// Bind the result columns to PHP variables
mysqli_stmt_bind_result($stmt, $Name, $Title);
// Create a new XML document in memory
$xw = new xmlWriter();
$xw->openURI('php://output');
$xw->openMemory();
$xw->startDocument('1.0');
// Start the outer data container
$xw->StartElement('rss');
$xw->WriteAttribute('version', '2.0');
// Fetch values
while (mysqli_stmt_fetch($stmt)) {
{
$xw->startElement('item');
// Write out the elements
$xw->writeElement('Name', $Name);
$xw->writeElement('Title', $Title);
$xw->endElement();
}
// End container
$xw->endElement();
// End the document
$xw->endDocument();
//header('Content-Type: text/xml');
print $xw->outputMemory(true);
// Close the database statement
mysqli_stmt_close($stmt);
// Close the database connection
mysqli_close($link);
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://php.net/manual/en/function.ucwords.php 中的相关部分
对于您的查询,我将替换:
为:
然后将 while 循环更改为:
显然您需要对此进行修改,因为我不知道您的数据库架构。
更改 mysqli 内容的主要原因是,如果将来对数据库进行架构更改,则不能保证数据库列具有相同的顺序。
祝你好运!
The relevant section in http://php.net/manual/en/function.ucwords.php
For your query, I'd replace:
With:
Then change your while loop to:
Obviously you need to tinker with this since I don't know your database schema.
The main reason for changing the mysqli stuff is that you aren't guaranteed to have the same ordering of the database columns if you make schema changes to the database in the future.
Good luck!